是否可以将hover()绑定(bind)到具有指定选择器的文档,以便为具有匹配属性的动态添加的内容分配JS函数?
我一直在研究这个主题:JQuery .on() method with multiple event handlers to one selector和 Is 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/