简体   繁体   English

jQuery Live在IE中运行非常慢

[英]Jquery live runs very slow in IE

i have noticed that this line 我注意到这条线

    $('#opp-tabs input[type=text]:not(#newActionText), #opp-tabs textarea').live('keyup', function() {
        onFormChanged();
    });

runs extremly slow in IE8, it works fine in firefox and chrom but not in IE, it to be honest it is cos of word 'not', how can it define this different to run in normall speed in IE ? 在IE8中运行极慢,在Firefox和chrom中运行良好,但在IE中却无法运行,说实话这是'not'一词的余弦,如何定义这种不同以IE的正常速度运行?

You really should make sure that selectors are as lightweight as possible, especially if used in .live() methods. 您确实应该确保选择器尽可能轻巧, 尤其是.live()方法中使用时。 What happens there is, that the event actually gets bound to the document.body and it needs to check there whether or not a given event matches your original selector string. 发生的事情是该事件实际上绑定到document.body ,它需要检查给定事件是否与原始选择器字符串匹配。 That process can be pretty expensive and in your case it also needs to invoke Sizzle ** , which makes it even slower. 该过程可能会非常昂贵,在您的情况下,它还需要调用Sizzle ** ,这会使它变得更慢。

Best case solution, use .on() or .delegate() to limit the necesarry DOM bubbling. 最佳情况解决方案,请使用.on().delegate()来限制.delegate() DOM冒泡。 That means, you don't bind the event handler to the body , but to the closest shared parent node , which increases overall performance. 这意味着,您无需将事件处理程序绑定到body ,而是绑定到最接近的共享父节点 ,从而提高了整体性能。 Secondly, more important, improve that selector ! 其次,更重要的是, 改进选择器

Use class names or whatnot to query for the nodes you require, actually it can't be much worse than the current state. 使用类名或其他名称查询所需的节点,实际上这不会比当前状态差很多。

To optimize the current form, try it like so: 要优化当前表单,请尝试如下操作:

$('#opp-tabs input:text, #opp-tabs textarea').not('#newActionText').on('keyup', 'closest shared parent selector', function() {});

Reference: .delegate() , .on() 参考: .delegate() .on()

** Sizzle is jQuery's javascript css-selector engine ** Sizzle是jQuery的javascript CSS选择器引擎

Adding to @jAndy's answer, you could represent this as 除了@jAndy的答案,您可以将其表示为

$('#opp-tabs input[type=text], #opp-tabs textarea').live('keyup', function() {
    if(this.id != "newActionText") onFormChanged();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM