Javascript jQuery live()已弃用:是否将on用于mouseenter和mouseout?
我想知道如何使用jQueryJavascript jQuery live()已弃用:是否将on用于mouseenter和mouseout?,javascript,events,deprecated,jquery,Javascript,Events,Deprecated,Jquery,我想知道如何使用jQueryon()重写以下侦听器 有什么想法吗 $(document).on('hover', '.box', function(e) { if( e.type == 'mouseenter') { $(this).children('.menu').stop(true, true).fadeIn(fadeIn); } else { $(this).children('.menu').stop(true, true).fadeOut(fadeOut);
on()重写以下侦听器
有什么想法吗
$(document).on('hover', '.box', function(e) {
if( e.type == 'mouseenter') {
$(this).children('.menu').stop(true, true).fadeIn(fadeIn);
} else {
$(this).children('.menu').stop(true, true).fadeOut(fadeOut);
}
});
与其使用document
,不如使用.box
的任何非动态父元素
阅读有关的信息
代理事件(也称为实时事件)的.on()
语法为:
对于精确的。在
等效项上:
$(document).on({
mouseenter: function() {
$(this).children('.menu').stop(true, true).fadeIn(fadeIn);
},
mouseleave: function() {
$(this).children('.menu').stop(true, true).fadeOut(fadeOut);
}
}, '.box');
虽然这里的主要好处是您不需要使用文档
,但您可以使用保证在页面生命周期内存在的最接近的父级。请参阅和文档。您将在那里找到有关如何将.live
转换为的信息。
$( StaticParent ).on( eventName, target, handlerFunction );
$(document).on({
mouseenter: function() {
$(this).children('.menu').stop(true, true).fadeIn(fadeIn);
},
mouseleave: function() {
$(this).children('.menu').stop(true, true).fadeOut(fadeOut);
}
}, '.box');