我需要为一些动态创建的元素添加一个 jquery 侦听器。我一辈子都无法让它发挥作用。tag_options 不是动态的,它的子项是动态的。我尝试了三种方法:
<div id = "tag_options">
<div class = "tag_option">hover me</div>
</div>
js:
// never works
$('#tag_options').delegate('.tag_option', 'hover', function(event){
alert();
});
// never works
$("#tag_options").on("hover", "[class='tag_option']", function(event) {
alert();
});
// works if not dynamically created
$('#tag_options .tag_option').hover( function(){
alert();
});