jQuery查找与上下文选择 [英] jquery find versus context selection
问题描述
具有以下html代码段
Having the following html snippet
<div class="something">
<p>Some text</p>
</div>
<div class="somethingElse">
<p>some other text</p>
</div>
我认为以下jquery片段是相同的(将具有相同的结果):
I think the following jquery snippets are identical (will have the same result):
$(".something").find("p").css("border", "1px solid red");
$("p", ".something").css("border", "1px solid red");
我的问题是,一个片段是否比另一个更好?
My question is, whether one snippet is better than the other and should be used
推荐答案
调用不相同.
据布兰登·亚伦(Brandon Aaron)称,他显然从事jQuery工作,并且还根据实时测试进行了测试这里,find方法总是更快.在下面的屏幕截图中查看结果.如果我缺少某些东西,请发表评论.
According Brandon Aaron, who apparently worked on jQuery, and also according to the live tests here, the find method is always faster. See results in the screenshot below. Please comment if I am missing something.
根据浏览器的不同,速度相差10%或更大,显然值得使用find.
With a 10% or greater difference in speed, depending on browser, it definitely seems worth using find.
在布兰登(Brandon)网站上的进一步解释是此处.
Further explanation at Brandon's site is here.
这篇关于jQuery查找与上下文选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!