简体   繁体   English

在下面的列表中哪一个用于.click的性能更好

[英]Which one is better performance to use for .click in the below list

Please vote for me which one in the below list is better? 请为我投票,以下列表中哪一个更好?

I have HTML: 我有HTML:

<div id="container">
    <button class="btn">Click Here 1</button>
    <button class="btn">Click Here 2</button>
    <button class="btn">Click Here 3</button>
    <button class="btn">Click Here 4</button>
    <button class="btn">Click Here 5</button>
    <button class="btn">Click Here 6</button>
    <!-- A lot of buttons -->
    <button class="btn">Click Here n - 2</button>
    <button class="btn">Click Here n - 1</button>
    <button class="btn">Click Here n</button> 
</div>

And Javascript with jQuery is: 和jQuery的Javascript是:

Case 1.1: 案例1.1:

$(".btn").click(function(e){
    //@todo something here
});

Case 1.2: 案例1.2:

var doSomething = function(e)
{
    //@todo something here
}
$(".btn").click(doSomething);

Case 2: 案例2:

$("#container").click(function(e){
    if( $(e.target).is(".btn") )
    {
        //@todo something here
    }
});

I am confused a litle bit what are different between them? 我很困惑他们之间有什么不同?

You should use the new jQuery on() function 您应该使用新的jQuery on()函数

$(document).on("click", ".btn", doSomething);

so that 以便

  • dynamically added content will be covered 动态添加的内容将被涵盖
  • jQuery won't have to waste time searching out, and wrapping all affected buttons with the handler; jQuery不必浪费时间搜索,并用处理程序包装所有受影响的按钮; all clicks will propogate up to the context, and jQuery will apply them if they match the selector— .btn in this case 所有点击都会传播到上下文,如果jQuery匹配选择器 - .btn ,jQuery将应用它们

If you know that all your buttons will be in #container, then you would use that as the context, instead of document. 如果您知道所有按钮都在#container中,那么您将使用作为上下文,而不是文档。

$("#container").on("click", ".btn", doSomething);

Case 1.1 and 1.2 are essentially the same. 案例1.1和1.2基本相同。 The only thing you gain from 1.2 is that you can call the function independently. 你从1.2中获得的唯一一点就是你可以独立调用这个函数。 If this is a need, then 1.2 should be used, if not, either is fine. 如果这是需要的话,那么应该使用1.2,否则就可以了。

Case 2 is actually not functionally equivalent to the others. 案例2实际上在功能上与其他案件不同。 This is essentially equivalent to live . 这基本上等同于live Using click will only attach to matching elements at the time click is called. 利用click将只重视当时匹配的元素click被调用。 Case 2 (and live ) will match the selector at the time the event happens. 情况2(和live )将在事件发生时匹配选择器。 If any .btn elements have been added after this code is called. 如果在调用此代码后添加了任何.btn元素。 Case 2 will let you click on them as well. 案例2也允许您点击它们。

EDIT: Note that in 1.7 live is replaced by on with certain parameters. 编辑:请注意,1.7 live被替换为on with some parameters。

Case 2 would be the best if you plan to use doSomething somewhere else as well. 如果您计划在其他地方使用doSomething情况2将是最好的。 If not, both 1 and 2 are equally good. 如果不是,1和2都同样好。 Of course, only talking style-wise... functionality is the same. 当然,只谈风格......功能是一样的。

Case 1.1 is a common anonymous function for your event handler and is the most often used case. 案例1.1是您的事件处理程序的常见匿名函数,并且是最常用的案例。

Case 1.2 you would use if you also want to call the function directly, not only as an event handler, so you need some way to refer to it. 如果您还想直接调用函数,而不仅仅是作为事件处理程序,那么您将使用案例1.2,因此您需要某种方式来引用它。

Case 2 is useful if you have a very large number of things, because it creates only one event handler instead of many. 如果你有很多东西,情况2很有用,因为它只创建一个事件处理程序而不是很多。 This case will also catch new items of class btn that are dynamically added to #container 这个案例还将捕获动态添加到#container的类btn新项

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

相关问题 在访问数组长度方面,哪一种性能更好 - Which one is better in performance in accessing length of array jQuery on()方法-哪种使用方式对性能更好? - jQuery on() method - which way to use is better for performance? 哪一种是在div上设置悬停事件的性能更好的方法? - Which one is the better way for performance to set a hover event on a div? 这些ArrayToMap函数用法中的哪一种在性能方面更好? - Which one of these ArrayToMap functions usages is better performance-wise? 正确使用以下语句性能的正确方法是哪种? - Which is right way to use below statement performance wise? &#39;__doPostBack&#39;或&#39;.click()&#39;哪个更好? - '__doPostBack' OR '.click()' Which is better? 为了获得更好的性能,哪个更好:componentWillUpdate或componentDidUpdate? - For better performance, which is better: componentWillUpdate or componentDidUpdate? 在内存使用和效率方面,使用变量或WITH关键字哪个更好? - in terms of memory usage and efficency, which one is better to use, a variable or WITH keyword? 是否更好地使用.delegate()性能? - Is it better to use .delegate() performance wise? 哪一个具有更好的性能:在每个渲染VS上添加和删除事件侦听器VS运行useEffect来更新ref - Which one has better performance: add and remove event listener on every render VS running an useEffect to update a ref
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM