javascript - 如何在鼠标进入 div 时运行函数,反之亦然?

标签 javascript jquery html

所以我试图在鼠标悬停在 div 上时运行一个函数,当鼠标离开 div 时,它运行另一个函数。

我已经尝试过使用 .hover(); 函数,但不起作用。

<div id="test">
</div>

$("#test").hover(function() {
  window.alert("it worked!")
})

我对 jQuery 还很陌生,大约是上个月开始的。谢谢!

最佳答案

hover()

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/

相关文章:

javascript - Knockout.js 跟踪选中的复选框

javascript - Node.js 中的同步 for in 循环

javascript - 如何获取文本框中的<td>值

jquery - jquery mobile 中的左垂直选项卡

javascript - 如何克服asp.net mvc3中的ajax数据大小限制

javascript - 使用 Workbox 启动运行时服务工作线程缓存

javascript - 如果textarea是动态的,如何从textarea中删除所有html标签?

jquery - 如何在内容中设置 jQuery 选项卡全屏加溢出滚动

jquery - 使用 Bootstrap 的粘性导航栏?

html - 表格的无边框标题