CSS 为什么要使用属性选择器来匹配类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15604182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use an attribute selector to match classes?
提问by ducin
I have found an exampleof responsive email templates where there are such CSS selectors such as the following:
我找到了一个响应式电子邮件模板的例子,其中有这样的 CSS 选择器,如下所示:
a[class="btn"]
Why is this syntax used if it's totally the same as:
如果与以下语法完全相同,为什么要使用此语法:
a.btn
Does it have any impact on mobile browsers or anything else?
它对移动浏览器或其他任何东西有影响吗?
回答by Eric
The []
syntax is an attribute selector.
该[]
语法是属性选择。
a[class="btn"]
This will select any <a>
tag with class="btn"
. However, it will notselect <a>
which has class="btn btn_red"
, for example (whereas a.btn
would). It only exactly matchesthat attribute.
这将选择<a>
带有class="btn"
. 但是,它不会选择<a>
哪个具有class="btn btn_red"
,例如(而a.btn
将)。它只与该属性完全匹配。
You may want to read The 30 CSS Selectors you Must Memorize. It's invaluable to any up-and-coming web developer.
您可能需要阅读您必须记住的 30 个 CSS 选择器。对于任何有前途的 Web 开发人员来说,它都是无价的。