$(this).find(“";p";).text()vs$(p";,$(this)).text()在jQuery中考虑速度

$(this).find(“";p";).text()vs$(p";,$(this)).text()在jQuery中考虑速度,jquery,performance,Jquery,Performance,如果我有这样一个简单的HTML布局: <div class="items"> <p>Some text #1</p> </div> . . <div class="items"> <p>Some text #n</p> </div> 酷,我想你在找。查找方法vs上下文方法 检查此项(因为您非常渴望) 上下文被转换为.find,因此如果您想避免这种情况,请使用.find 其余的

如果我有这样一个简单的HTML布局:

<div class="items">
    <p>Some text #1</p>
</div>
.
.
<div class="items">
    <p>Some text #n</p>
</div>

酷,我想你在找
。查找
方法vs
上下文方法

检查此项(因为您非常渴望)

上下文被转换为
.find
,因此如果您想避免这种情况,请使用
.find

其余的jspref将显示性能


希望它符合你的需要<代码>:)
虽然两个调用都是相同的。

$(“p”,$(this))
应该是
$(“p”,this)
,它代表
$(this)。查找(“p”)
。它们是相等的。如果你一次做的次数少于几千次,实际的速度差可以忽略不计。你自己看看:避免一起找<代码>$(“.items p”)
在处理10k元素时,它将比.find快得多。@NULL:它是如何重复的?
#1.Approach
$(".items").each(function() {
    var p = $(this).find("p").text();
    //do stuff with p
});

#2.Approach
$(".items").each(function() {
    var p = $("p", $(this)).text();
    //do stuff with p
});