Can you target an element with CSS only if 2 classes are present?(仅当存在 2 个类时,您可以使用 CSS 定位元素吗?)
问题描述
您可能已经知道,元素上可能有多个类,以空格分隔.
As you probably already know, you may have multiple classes on elements separated by a space.
<div class="content main"></div>
而使用 CSS,您可以使用 .content 或 .main 来定位该 div.如果且仅当两个类都存在时,有没有办法定位它?
And with CSS you can target that div with either .content or .main. Is there a way to target it if and only if both classes are present?
<div class="content main">I want this div</div>
<div class="content">I don't care about this one</div>
<div class="main">I don't want this</div>
我将使用哪个 CSS 选择器仅获取第一个 div(假设我不能使用 .content:first-child 或类似的)?
Which CSS selector would I use to get the first div only (assume I can't use .content:first-child or similar)?
推荐答案
是的,只需将它们连接起来:.content.main.请参阅 CSS 类选择器.
Yes, just concatenate them: .content.main. See CSS class selector.
但请注意,最高版本 6 的 Internet Explorer 不支持多个类选择器,仅支持最后一个类名.
But note that the Internet Explorer up to version 6 doesn’t support multiple class selectors and just honors the last class name.
这篇关于仅当存在 2 个类时,您可以使用 CSS 定位元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅当存在 2 个类时,您可以使用 CSS 定位元素吗
基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
