所以我试图在鼠标悬停在 div 上时运行一个函数,当鼠标离开 div 时,它运行另一个函数。
我已经尝试过使用 .hover();
函数,但不起作用。
<div id="test">
</div>
$("#test").hover(function() {
window.alert("it worked!")
})
我对 jQuery 还很陌生,大约是上个月开始的。谢谢!
最佳答案
Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.
正是第一个用于mouseenter
,第二个用于mouseleave
。
工作代码示例:
$("#test").hover(function() {
console.log("Entered!")
}, function() {
console.log("left!")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="test">Test
</div>
关于javascript - 如何在鼠标进入 div 时运行函数,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373659/