是否有优势,在CSS中使用非常具体的选择? [英] Is there an advantage to using very specific selectors in CSS?
问题描述
据我了解,jQuery中,这是有利的使用选择时,更具体,这样的jQuery不必遍历整个DOM找到你要找的内容。例如, $('span.description')
不仅仅是更好的 $('说明')
如果我知道说明
类永远只能适用于<跨度方式>
元素
I understand that in jQuery, it's advantageous to be more specific when using selectors so that jQuery doesn't have to traverse the entire DOM to find what you're looking for. For example, $('span.description')
is better than just $('.description')
if I know that the description
class is only ever applied to <span>
elements.
这是用CSS的情况下,也?有我使用任何特定的优势 span.description {}
而不是的.DESCRIPTION {}
?我想在速度,优化等方面我是节省了浏览器,告诉它究竟在何处寻找任何工作?
Is this the case with CSS, too? Is there any specific advantage for me to use span.description { }
instead of .description { }
? I'm thinking in terms of speed, optimization, etc. Am I saving the browser any work by telling it exactly where to look?
推荐答案
这是真正的在CSS -
This is true in CSS -
一个很好的规则是从最近的ID下降。
ID被索引,以便它们的定位速度非常快。没有理由在选择器使用一个以上的
A good rule is to descend from the nearest ID. IDs are indexed so locating them is extremely fast. There is no reason to use more than one in your selector.
Google Code- Optimize browser rendering
这回答了很多问题,我对这个问题,包括这一单
我希望你觉得它有用。
This answered a lot of questions I had on the subject including this one- I hope you find it useful.
这篇关于是否有优势,在CSS中使用非常具体的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!