我正在动态添加一些元素,并在委托事件处理程序中为其分配了一个悬停属性,我在下面的代码中为其使用了它,但它不起作用。
$(document).on("hover", ".sec_close_fast", function() {
$(this).parent('div').parent('div').css("border", "3px solid #000000");
});
然后我用了mouseover
,它起作用了:
$(document).on("mouseover", ".sec_close_fast", function() {
$(this).parent('div').parent('div').css("border", "3px solid #000000");
});
我想知道为什么hover
不行,但行得通mouseover
。
函数/事件.hover
实际上不是事件,而只是mouseenter
and的简写mouseleave
。从文档:
该
.hover()
方法为mouseenter
和mouseleave
事件绑定处理程序。您可以使用它在鼠标位于元素内时将行为简单地应用于元素。
因此,您不能使用它来“委托”事件。
解决方案
正如您已经提到的以及在文档中提到的那样,您可以使用:
$(static_parent).on("mouseenter mouseleave", element, function (e) {
if (e.type == "mouseenter") {
// check if it is mouseenter, do something
} else {
// if not, mouseleave, do something
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句