1
nigelvon 2014-03-11 12:39:58 +08:00 1
因为解释器是从右往左解释的。
|
2
zzNucker 2014-03-11 12:40:03 +08:00 1
这个问题。。。 你搜一下CSS匹配方式就知道了。
|
3
slixurd 2014-03-11 12:43:10 +08:00 1
一拖出去搜 #footer > h3 low performance就搜到google develops的文章了
而且也有解释,实际上就是后代选择器,子元素/兄弟选择器效率低而已 Descendant selectors are inefficient because, for each element that matches the key, the browser must also traverse up the DOM tree, evaluating every ancestor element until it finds a match or reaches the root element. The less specific the key, the greater the number of nodes that need to be evaluated. 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. |
4
Part OP |
5
learnshare 2014-03-11 12:52:43 +08:00 1
要让选择器匹配到的元素最少,或者匹配方式(如从右到左)最直接(就是少用嵌套、* 或者其他关系选择器)。
效率高不高,是要看浏览器的匹配算法的。有兴趣可以去研究浏览器原理/算法相关的东西,挺复杂的。 |
6
otakustay 2014-03-11 14:15:56 +08:00 1
保证最右边那个是tag、#id或.class之一,效率就高了,其它情况([attr]、*之类)效率就低
所以光说#footer > h3,这货效率一点不低,因为>只要往上找一级父元素就行,也没有更多更复杂的selector存在 |