javascript - 在动态创建的元素上使用hover()

标签 javascript jquery

我想在动态创建的元素上使用 jQuery 的 hover() 。我尝试了以下但没有成功。我怎样才能做到这一点?我无法使用 mouseentermouseleave 因为此页面将嵌入到 iframe 中。

$(document).on('hover', '.dynamic', function(){
    //do
});

最佳答案

hover()不是一个事件,它是一个用于注册 mouseenter 的实用方法和 mouseleave事件处理程序。

The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.

因此您可以对动态元素使用 mouseenter 和 mouseleave 事件处理程序

$(document).on('mouseenter', '.dynamic', function () {
    //do
}).on('mouseleave', '.dynamic', function () {
    //do
});
<小时/>

如果您想为两者使用一个处理程序,那么

$(document).on('mouseenter mouseleave', '.dynamic', function () {
    //do
});

关于javascript - 在动态创建的元素上使用hover(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237686/

相关文章:

javascript - 当函数取消隐藏 2 个元素时如何在 javascript 中显示/隐藏?

javascript - 当先前的输入值发生更改时,如何仅获取当前输入的值?

javascript - 选择具有相同类的多个div

Node.js 中未解析的 JSON 数据在控制台中显示未定义

jsp - 如何使用 Ajax 和 Servlet 制作登录表单

javascript - 如何在 jQuery 中实现可排序的播放列表

javascript - DataTables 的格式正确的表抛出错误

javascript - 如何显示来自代码隐藏的警报消息?

javascript - 防止元素被 CSS 操纵

javascript - JS 应用程序自动刷新/元素消失编辑