对于动态创建的内容,我使用“on”方法来处理事件。这适用于点击但不适用于悬停。
代码如下:
$(document).on('click', 'li', function(){
alert('a');
});
有效但是
$(document).on('hover', 'li', function(){
alert('a');
});
没有。
代码有错吗?或者问题是由于我的设置中的其他原因引起的。
最佳答案
As of 1.9, the event name string "hover" is no longer supported as a synonym for "mouseenter mouseleave"
你需要使用mouseenter
和mouseleave
事件
$(document).on('mouseenter', 'li', function(){
alert('a');
});
$(document).on('mouseleave', 'li', function(){
alert('a');
});
你也可以使用替代语法
$(document).on({
mouseenter: function () {
},
mouseleave: function () {
}}, "li");
关于javascript - jQuery 悬停事件处理程序在单击时不处理动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41236251/