本文介绍了如何在 CSS 选择器中组合类和 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以下 div:
If I have the following div:
<div class="sectionA" id="content">
Lorem Ipsum...
</div>
有没有办法定义一种样式来表达具有 id='content'
AND class='myClass'
的 div"这一理念?
Is there a way to define a style that expresses the idea "A div with id='content'
AND class='myClass'
"?
或者你只是像在
<div class="content-sectionA">
Lorem Ipsum...
</div>
或者
<div id="content-sectionA">
Lorem Ipsum...
</div>
推荐答案
在你的样式表中:
div#content.myClass
这些也可能有帮助:
div#content.myClass.aSecondClass.aThirdClass /* Won't work in IE6, but valid */
div.firstClass.secondClass /* ditto */
并且,根据您的示例:
div#content.sectionA
编辑,4 年后:因为这是超级旧的并且人们一直在寻找它:不要在你的选择器中使用 tagNames.#content.myClass
比 div#content.myClass
快,因为 tagName 添加了您不需要的过滤步骤.只在必须的地方使用选择器中的标签名!
Edit, 4 years later: Since this is super old and people keep finding it: don't use the tagNames in your selectors. #content.myClass
is faster than div#content.myClass
because the tagName adds a filtering step that you don't need. Use tagNames in selectors only where you must!
这篇关于如何在 CSS 选择器中组合类和 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!