简体   繁体   English

jQuery在获取属性值方面的性能

[英]jQuery performance in terms of getting attribute value

Is there any difference in performence between these three below when it comes to getting element's attribute value? 在获取元素的属性值时,下面这三个之间的性能有何不同?

a) attr() function a)attr()函数

$('div').click(function() {
    var div_id = $(this).attr('id');
    // rest of the logic
});

b) event object's target property b)事件对象的目标属性

$('div').click(function(e) {
    var div_id = e.target.id;
    // rest of the logic
});

c) pure JS approach c)纯JS方法

$('div').click(function() {
    var div_id = this.id;
    // rest of the logic
});

No doubt option c is way better as given with others : 无疑,选项c与其他选项相比会更好:

$('div').click(function() {
   var div_id = this.id;
   // rest of the logic
});

because this is available in the browser itself and you are not using another method of any external library like the other two. 因为这在浏览器本身中可用,并且您没有像其他两个库那样使用任何外部库的其他方法。

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

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