Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery.live和jQuery.delegate之间的差异_Javascript_Jquery_Event Delegation - Fatal编程技术网

Javascript jQuery.live和jQuery.delegate之间的差异

Javascript jQuery.live和jQuery.delegate之间的差异,javascript,jquery,event-delegation,Javascript,Jquery,Event Delegation,我读过一些关于为什么不使用jQuery.live()的帖子,我想检查一下我是否得到了它:) 当我调用$(“body”).delegate('element','click',函数) 它是否与$(element.live('click',function)相同? 在正常行为的情况下..根据帖子,有一些stopPropagation和性能提升,但主要区别是live每次都绑定到body元素,而delegate可以绑定到另一个元素?有一个屏幕广播来解释这一点: 从网站上引用: // Live(), in

我读过一些关于为什么不使用jQuery.live()的帖子,我想检查一下我是否得到了它:) 当我调用
$(“body”).delegate('element','click',函数)

它是否与
$(element.live('click',function)
相同? 在正常行为的情况下..根据帖子,有一些stopPropagation和性能提升,但主要区别是live每次都绑定到body元素,而delegate可以绑定到另一个元素?

有一个屏幕广播来解释这一点:

从网站上引用:

// Live(), introduced in 1.3, allows for the binding  
// of event handlers to all elements that match a  
// selector, including those created in the future.  
// It does this by attaching the handler to the document.  
// Unfortunately, it does not work well with chaining.  
// Don't expect to chain live() after calls like  
// children().next()...etc.  
$("li").live("click", function() {  
    $(this).parent().append("<li>New Element</li>");  
});   

// Delegate, new to version 1.4, perhaps should have been a complete  
// replacement for Live(). However, that obviously  
// would have broken a lot of code! Nonetheless,  
// delegate remedies many of the short-comings  
// found in live(). It attaches the event handler  
// directly to the context, rather than the document.  
// It also doesn't suffer from the chaining issues  
// that live does. There are many performance benefits  
// to using this method over live().  
$('#items').delegate('li', 'click', function() {  
    $(this).parent().append('<li>New Element</li>');  
});   
//1.3中引入的Live()允许绑定
//将事件处理程序添加到与
//选择器,包括将来创建的选择器。
//它通过将处理程序附加到文档来实现这一点。
//不幸的是,它不能很好地用于链接。
//不要期望在像这样的调用之后链接live()
//children().next()…等。
$(“li”).live(“单击”,函数(){
$(this.parent().append(“
  • 新元素”
  • ”); }); //新版本1.4的委托,可能应该是一个完整的 //替换Live()。但是,很明显, //会破坏很多代码!尽管如此, //委派弥补了许多不足 //在live()中找到。它附加事件处理程序 //直接指向上下文,而不是文档。 //它也不会受到链接问题的影响 //生活就是这样。有许多性能优势 //无法在live()上使用此方法。 $('#items')。委托('li','click',function(){ $(this.parent().append(“
  • 新元素”
  • ”); });
    由于.live()方法处理传播到文档顶部的事件,因此无法停止实时事件的传播。类似地,由.delegate()处理的事件将始终传播到它们被委托到的元素;在调用委托的事件处理程序时,它下面的任何元素上的事件处理程序都已经执行。

    一个重要的区别是,.live()实际上会为初始选择器构建jQuery元素列表,即使.live()函数本身只需要选择器字符串。这意味着,如果选择器有点昂贵,那么设置处理程序的代码将毫无理由地在整个DOM中运行。“.delegate()”调用不执行该操作

    我真的不认为新代码应该使用“.live()”;这在某种程度上是一个建筑上的错误,最终应该悄然消亡

    live每次都绑定到body元素,而delegate可以绑定到另一个元素,这是主要的区别吗


    是的,没错。假设您有一个表,可以从中添加和删除行,并且希望处理对这些行的单击(或行中的链接或按钮)。你可以使用
    live
    来实现这一点,但随后事件必须一直持续到身体层面,让我们面对它,它感觉有点像一个全局变量。如果您在
    元素上使用
    委托
    ,您将保持更具针对性,与页面上发生的其他事情隔离开来
    delegate
    live

    的一个更加模块化、包含式的版本,其缺点是
    .live
    在文档级别运行,
    .delegate
    在您指定的任何元素上运行。为什么会有不同?如果使用
    .live
    绑定了一个(或多个)mousemove事件,jQuery将在每次将鼠标移动到页面上任何位置时执行代码,以查看回调函数是否应运行。这是非常低效的,并且是使用
    .delegate
    的原因
    .delegate
    函数仅在指定的dom节点内部产生偶数时运行。例如,如果您说
    $('ul#myUL').delegate(…)
    ,那么jQuery将只检查当事件起源于
    ul#myUL

    时代码是否应该运行,实际上
    。delegate
    这样做,它只是从原始选择器的上下文中执行,这使得它更有效。而且仍然有理由使用
    .live()
    。以这个网站为例——拥有
    $('.userInfo').live()
    在你拥有用户名的几十个地方中的任何一个显示关于用户的信息都是非常有意义的。哈哈!今天我调试了一个$.map()调用,该调用试图过滤二维数组,结果却发现数组返回的“功能”被展平为结果…@cWolfs它所要做的就是像
    $(event.target).is(selector)
    -它不必查看整个DOM。@Pointy-
    .live()
    也可以这样做。唯一的区别是,
    .delegate()
    被触发的频率较低。@Pointy-对不起,误解了您最初的帖子。您可以随时直接调用
    $.fn.live
    并绕过它:)