是否有优势,在CSS中使用非常具体的选择? [英] Is there an advantage to using very specific selectors in CSS?

查看:111
本文介绍了是否有优势,在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.

谷歌code-优化浏览器中呈现

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

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