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

jQuery performance in terms of getting attribute value

本文关键字:方面 性能 属性 获取 jQuery      更新时间:2023-09-26

在获取元素的属性值时,下面这三种方法的性能有什么不同吗?

a) attr() function

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

b)事件对象目标属性

$('div').click(function(e) {
    var div_id = e.target.id;
    // rest of the logic
});
c)纯JS方法
$('div').click(function() {
    var div_id = this.id;
    // rest of the logic
});

毫无疑问,c选项与其他选项相比要好得多:

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

,因为这在浏览器本身是可用的,并且您没有使用任何外部库的其他方法,如其他两个。