查询jQuery's $(selector).each() 'element'参数vs $(th
Difference between getting jQuery's $(selector).each() 'element' parameter, vs $(this)
获得jQuery .each()
的element
参数是否有任何差异(与性能或其他方面有关)
$(".element").each(function(i, e){
console.log("element: " + $(e));
});
和使用$(this)
?
$(".element").each(function(){
console.log("element: " + $(this));
});
我做了几次测试,在编程上我没有注意到任何差异。我一直使用$(this)
,因为它是大多数应用程序使用的标准。
不,没有实际区别。在each
的源代码中,我们可以看到同样的东西(obj[i]
)被传递给call
,用作this
和回调中的第二个参数:
value = callback.call(obj[i], i, obj[i]);
虽然在您的示例中没有实际差异,但在封闭的函数或方法调用中使用e
是一个有用的特性。
$('.element').each(function(i, e) {
$('.otherElement').text(function(index, element){
// in this method, $(this) is an element from
// the $('.otherElement) collection (also $(element));
// to access elements of the $(`.element') collection
// we can no longer use $(this), but we can still use
// $(e) (or e):
return $(e).eq(index).text(); // will set the text of the $(element)
// to the text of the $(e) element
});
})
相关文章:
- setInteval vs setTimeout
- $(this).prop('property') vs. this.property
- Js.erb VS按钮标记-不'不起作用.为什么?
- reactjs this.refs vs document.getElementById
- Performance: NaCl vs Emscripten
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- Advantages to DOMParser vs template & innerHTML
- JavaScript-===vs===运算符性能
- 开发第三方小部件-Angular vs jQuery vs普通的旧JS
- PhantomJS(vs nightwatch.js)设置cookie错误
- Javascript events: window.event vs argument reference (funct
- 需要确认我对Servlet vs RESTful网页的理解's的差异
- "#"keyCode=222 vs 51(Chrome与Android版Chrome)-为什么有区别
- .notion vs.[]notation |插入到文本对象中
- Javascript-使用“”将toDateStringMonth从文本字符串解析为数字字符串;如果“;vs“;开关”;
- Appending the innerhtml of <th>
- 没有框架/DLL的VS Web应用程序项目
- Eval vs execscript
- VS代码:在<脚本>标签
- 查询jQuery's $(selector).each() 'element'参数vs $(th