在文档就绪时,我将此事件处理程序附加到具有类bubbleItemOff的所有元素.我的问题是,一些bubbleItemOff元素是在触发文档就绪事件后动态创建的.
有没有办法自动将事件处理程序添加到新创建的元素中,或者在创建元素后是否必须显式执行此操作?
$(function() {
$('.bubbleItemOff').mouseenter(function(e)
{
//...
});
});
Run Code Online (Sandbox Code Playgroud)
您可以on在委托事件方法中使用jQuery 方法:
$(".parentItem").on("mouseenter", ".bubbleItemOff", function(e) {
//
});
Run Code Online (Sandbox Code Playgroud)
这里.parentItem指的是任何父母.bubbleItemOff.