jQuery单选择器vs .find() [英] jQuery single selector vs .find()
本文介绍了jQuery单选择器vs .find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪个更适合用作效果视角:
Which is better to use as a performance perspective:
$(".div1 h2, .div1 h3")
或
$(".div1").find("h2, h3")
推荐答案
你的问题的答案是:是的。
The answer to your question is: yes.
不要担心性能差异,除非你的代码很慢即可。如果是,请使用分析器确定瓶颈。
Don't worry about the performance difference, unless your code is slow. If it is, use a profiler to determine bottlenecks.
从分析的角度来看:
$(".div1 h2, div1 h3")
应该更快,因为jQuery将通过 querySelectorAll
(如果存在)管道它,本机代码将比非本机代码运行得更快。它还可以节省额外的函数调用。
should be faster as jQuery will pipe it through querySelectorAll
(if it exists) and native code will run faster than non-native code. It will also save on an additional function call.
$(".div1").find("h2, h3")
如果你打算在 .div1上链接其他一些函数,那就更好了
:
$(".div1").find("h2, h3").addClass('foo').end().show();
这篇关于jQuery单选择器vs .find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文