简体   繁体   English

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

[英]difference between $(*[attr]) and $([attr])

I just want to know which one is preferable in the following $(*[attr]) and $([attr]) . 我只想知道下面的$(*[attr])$([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. 抱歉,如果我要重复一个。

I think Both are same in behavioral level. 我认为两者在行为水平上都是相同的。

As @satpal said in comment. 正如@satpal在评论中所说。

I tried in jsPref, It says $('*[onclick]') is the fastest. 我在jsPref中尝试过,它说$('*[onclick]')是最快的。

在此处输入图片说明

Thanks @satpal 谢谢@satpal

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

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