【发布时间】:2013-05-30 07:25:47
【问题描述】:
我正在读一本书,他们展示了几个如何在 DOM 上选择元素的示例,但他们建议始终在选择器上使用 Jquery 遍历方法,例如,如果您在 div 中有一个列表而不是使用
$("#myList > li")
你应该使用
$("#myList").children("li")
大多数时候我使用第一个而不是后者,作者说第二个更受欢迎且效率更高,但他没有说明原因,谁能解释这背后的原因?
【问题讨论】:
-
如果你对性能感兴趣,你根本不应该使用 jQuery。使用你更清楚的东西。
-
jQuery selector performance 的可能重复项
-
Read this
Child and adjacent selectors are inefficient because, for each matching element, the browser has to evaluate another node. It becomes doubly expensive for each child selector in the rule. Again, the less specific the key, the greater the number of nodes that need to be evaluated. However, while inefficient, they are still preferable to descendant selectors in terms of performance.
标签: javascript jquery jquery-selectors