简体   繁体   English

jQuery $(this)与变量

[英]jQuery $(this) vs. variable

Given: 鉴于:

var element = $('#element');

I'm wondering which is faster: 我想知道哪个更快:

element.click(function(){
    element.dosomething()
)}

Or: 要么:

element.click(function(){
    $(this).dosomething()
)}

Or does it matter? 或者重要吗?

Use element . 使用element

If element was a jQuery collection matching a single element, for example, $( someId ) , then just use it. 如果element是一个匹配单个元素的jQuery集合,例如$( someId ) ,那么就使用它。

If the selector was meant to match more than one element, then element is actually elements , a collection of elements, so, in that case you use $(this) inside your click handler to catch the one actually clicked. 如果选择器要匹配多个元素,那么element实际上是elementselements的集合,因此,在这种情况下,您在单击处理程序中使用$(this)来捕获实际单击的元素。

The difference is explained int he following examples: 以下示例解释了不同之处:

1- Handler on single element 1-单个元素处理程序

var table = $("#myTable");
table.click(function() {
    // Same as $(this), except $(this) creates another
    //  wrapper on the same object (which isn't too expensive anyway)
    table.doSomething();
});

2- Handler on multiple elements 2-处理多个元素

var rows = $("#myTable > tbody > tr");
rows.click(function() {
    // Here we have to use $(this) to affect ONLY the clicked row
    $(this).doSomething();
});

3- Handler on single element, but called for multiple child elements 3-处理单个元素,但需要多个子元素

var table = $("#myTable");
// "on" and "live" call handler for only child elements matching selector
// (Even child elements that didn't exist when we added the handler, 
     as long as parent -table in this case- exists)
table.on("click", "tbody > tr", function() {
    // Here we have to use $(this) to affect ONLY the clicked row
    $(this).doSomething();
});

I find it assuring (and less work, a very tiny difference though) to just the existing wrapper, showing that I'm expecting a single element in this case and I'm just working with it. 我发现它确保(和更少的工作,虽然是一个非常小的差异)只是现有的包装器,显示我在这种情况下期待一个元素,我只是在使用它。 And use $(this) when I'm dealing with elements of a collection of matching elements. 当我处理匹配元素集合的元素时,使用$(this)

The speed would probably be the same, but using $(this) is much better because you don't have to worry about element being reassigned to something else (or the value of element being lost entirely). 速度可能是相同的,但使用$(this)要好得多,因为你不必担心元素被重新分配给其他东西(或元素的值完全丢失)。

Also, if you refactor and use a selector for a class instead of a specific element, the function will work for all matched elements, not just the one. 此外,如果您重构并使用选择器而不是特定元素,则该函数将适用于所有匹配的元素,而不仅仅是一个元素。

The first is faster. 第一个是更快。 The second runs the same selector twice. 第二个运行相同的选择器两次。 That said, you will only use that code once using the first method, it's probably not what you want most of the time. 也就是说,一旦使用第一种方法,您将只使用该代码,这可能不是您大多数时候想要的。

In practice, use a pattern like: 在实践中,使用如下模式:

$('stuff').click(function(){
    var $$ = $(this); 
    $$.dosomething();
    $$.dosomethingelse();
)}

That is, unless you only use a selector once, assign it to a variable first. 也就是说,除非您只使用一次选择器,否则首先将其分配给变量。

Well jQuery has it's unique cache of dom elements(that already been touched once by jquery) so actually in most cases this won't make a real difference. 好吧jQuery有它独特的dom元素缓存(已经被jquery触摸过一次)所以实际上在大多数情况下这不会产生真正的区别。

I do not really believe this is your case though, jquery will actually wrap the this which is the element, so you are not really running a query of any kind twice. 我真的不相信这是你的情况,jquery实际上会包装这个元素,所以你并没有真正运行任何类型的查询两次。

BTW , in some cases this does make a difference(when delegating for instance). 顺便说一句 ,在某些情况下,这确实有所不同(例如,委托时)。

I would prefer the second. 我更喜欢第二个。 If you ever wanted to refactor the function and reuse it for another button it would be more portable. 如果您想重构该函数并将其重用于另一个按钮,那么它将更具可移植性。

当$(this)导致函数调用时,你的第一个例子在理论上更快,但可能不是很多。

This would be faster: 这会更快:

element.click(function(){
    element.dosomething();
)}

This would be fastest: 这将是最快的:

element.bind('click', element.dosomething);
  1. element is cached (and not changed, i assume) 元素被缓存(并且没有改变,我假设)
  2. use bind('click') directly don't use .click() wrapper 直接使用bind('click')不要使用.click()包装器
  3. if element.dosomething is the function you want to call, pass it directly without extra call 如果element.dosomething是您要调用的函数,则直接传递它而无需额外调用

I am assuming that element.dosomething() is correct and also element doesn't change. 我假设element.dosomething()是正确的, element也不会改变。

Here is an example of working code: http://jsfiddle.net/xhS3b/ 以下是工作代码示例: http//jsfiddle.net/xhS3b/

//<a href="#" id="element">click me</a>

var element = $('#element');
element.dosomething = function () {
    alert('did something');   
    return false;
}
element.bind('click', element.dosomething);

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

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