$(* [attr])和$([attr])之间的差异(difference between $(*[attr]) and $([attr]))

编程入门 行业动态 更新时间:2024-10-21 05:47:57
$(* [attr])和$([attr])之间的差异(difference between $(*[attr]) and $([attr]))

我只想知道以下$(*[attr])和$([attr])哪一个更好。 为什么?

因为两者都是一样的。

$('[onclick]').each(function(i,elem){
  console.log(elem.onclick);
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div onclick="asd()"></div>
<div onclick="def()"></div>
<div id="dv1"></div>
<div id="dv2"></div> 
  
 

$('*[onclick]').each(function(i,elem){
  console.log(elem.onclick);
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div onclick="asd()"></div>
<div onclick="def()"></div>
<div id="dv1"></div>
<div id="dv2"></div> 
  
 

还有浏览器支持呢。

如果我问复制品,请道歉。

I just want to know which one is preferable in the following $(*[attr]) and $([attr]). Why?

Because both are doing same.

$('[onclick]').each(function(i,elem){
  console.log(elem.onclick);
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div onclick="asd()"></div>
<div onclick="def()"></div>
<div id="dv1"></div>
<div id="dv2"></div> 
  
 

$('*[onclick]').each(function(i,elem){
  console.log(elem.onclick);
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div onclick="asd()"></div>
<div onclick="def()"></div>
<div id="dv1"></div>
<div id="dv2"></div> 
  
 

And also what about the browser support.

Apologize if i asked the duplicate one.

最满意答案

我认为两者在行为层面上是相同的。

正如@satpal在评论中所说。

我试过jsPref,它说$('*[onclick]')是最快的。

在此处输入图像描述

谢谢@satpal

I think Both are same in behavioral level.

As @satpal said in comment.

I tried in jsPref, It says $('*[onclick]') is the fastest.

enter image description here

Thanks @satpal

更多推荐

本文发布于:2023-08-01 02:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:差异   attr   difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!