【发布时间】:2015-07-19 08:39:46
【问题描述】:
假设有以下 HTML:
<div id="test">
<span class="testSpan"></span>
</div>
在#test div 中选择span 有两种方法
(我知道我可以直接选择跨度,但这不是问题的重点):
$('#test > .testSpan');
和
$('#test').find('.testSpan');
就速度、浏览器兼容性或其他方面而言,以下哪种方式最优化?
还是没有区别?
【问题讨论】:
-
也许您应该阅读以下内容:learn.jquery.com/performance/optimize-selectors 第一个示例非常适合您的问题:
The .find() approach is faster because the first selection is handled without going through the Sizzle selector engine – ID-only selections are handled using document.getElementById(), which is extremely fast because it is native to the browser.
标签: jquery html css jquery-selectors css-selectors