使用CSS子选择器会更快吗? [英] Would it be faster to use a CSS Child Selector?
问题描述
如果我们要定位段落中的链接,哪个选择器会更有效/更快?
If we wanted to target a link within a paragraph, which selector would be more efficient/faster?
p a
或
p > a
推荐答案
第二个(极其)快一点. CSS是由浏览器反向处理的,因此这两个规则都在页面上的所有a
元素上进行了测试.对于第二条规则,它仅需要测试直接父级,对于第二条规则,则需要测试整个后代链.
The second is (extremely) marginally faster. CSS is handled in reverse by browsers, so both your rules are tested on all a
elements on the page. For the second rule it only needs to test the direct parent, for the other one it would need to test the entire descendant chain.
在实践中,执行时间的差异在统计上不会很明显,直到您在具有相同数量的HTML行的页面上获得成千上万的此类内容为止.
In practice, the execution time difference won't be statistically significant until you get tens of thousands of these on a page with just as many lines of HTML.
这篇关于使用CSS子选择器会更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!