使用CSS子选择器会更快吗? [英] Would it be faster to use a CSS Child Selector?

查看:105
本文介绍了使用CSS子选择器会更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们要定位段落中的链接,哪个选择器会更有效/更快?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆