2017-08-06 81 views
1
document.getElementById("both-gif").onmouseover=function() {MouseOver()}; 

document.getElementById("both-gif").onmouseout=function() {MouseOut()}; 



function MouseOver() { 
    document.getElementById("both-gif").addClass("animated bounce") 
} 

function MouseOut() { 
    document.getElementById("both-gif").removeClass("animated bounce") 
} 

我试图让我的gif图像反弹,它有id“both-gif”。我究竟做错了什么?动画鼠标事件不起作用

+1

我们不知道没有你的HTML和CSS。请点击'<>'并创建一个[mcve] - 但第一个错误是在DOM元素 – mplungjan

回答

1
document.getElementById("both-gif").addClass("animated bounce") 

这里addClass是一个jQuery函数。它不会在JavaScript中工作。

你不能链jQuery的功能与document.getElementById 您需要将其更改为

document.getElementById("both-gif").classList.add("animated","bounce") 

而且这条线

document.getElementById("both-gif").removeClass("animated bounce") 

document.getElementById("both-gif").classList.remove("animated","bounce") 
+0

上使用jQuery感谢指点。没有我的错误。它需要一个逗号分隔列表 –

+1

只有较新的浏览器和[不是IE](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Browser_compatibility)支持添加多个参数。他们需要逗号分隔 - 我看到你的更新。 – mplungjan