[英]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是:
$(".btn").click(function(e){
//@todo something here
});
var doSomething = function(e)
{
//@todo something here
}
$(".btn").click(doSomething);
$("#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 以便
.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.