jQuery 将悬停绑定(bind)到 "document"

标签 jquery

是否可以将hover()绑定(bind)到具有指定选择器的文档,以便为具有匹配属性的动态添加的内容分配JS函数?

我一直在研究这个主题:JQuery .on() method with multiple event handlers to one selectorIs it possible to use jQuery .on and hover?

我目前正在使用:

$('.profile-gallery-image-container').on({
    mouseenter: function(){
        $('.delete-image', this).show();
        $('.image-options', this).show();
    },
    mouseleave: function(){
        $('.delete-image', this).hide();
        $('.image-options', this).hide();
    }
}, '.profile-gallery-image-container');

但无法使用匹配的选择器将功能传递给新内容。

最佳答案

在委托(delegate)事件方法中,您应该将事件绑定(bind)到 .profile-gallery-image-container 的一个父元素。一种可能的解决方案是将其绑定(bind)到 <body> (或 document )元素:

$('body').on({
    mouseenter: function(){
        $('.delete-image', this).show();
        $('.image-options', this).show();
    },
    mouseleave: function(){
        $('.delete-image', this).hide();
        $('.image-options', this).hide();
    }
}, '.profile-gallery-image-container');

关于jQuery 将悬停绑定(bind)到 "document",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162359/

相关文章:

jquery - SPA 自动获取隐藏的输入值并插入到查询中的参数中

jquery - 替换行时无法使 jQuery DataTable 中的数据无效

jquery - 自动滑动 jQuery jCarousel Lite 不工作

javascript - 语法错误: illegal character when posting array to data attribute of highcharts

php - 插入src URL后无法播放HTML5音频

javascript - 使用 jQuery 部分显示/隐藏元素

jquery - 使用 JQuery 动态生成时,XML 中的 HREF 内容未作为链接加载

javascript - 带悬停的 Jquery 工具提示

jquery - AJAX post 不发送数据到 laravel Controller

javascript - 为什么不使用each()创建多个标签?