Are parentheses allowed in CSS selectors?(CSS 选择器中是否允许使用括号?)
问题描述
在下面的示例中,我想创建一个仅适用于带有Blockhead"文本的标题的 CSS 规则.
<div class="gumby"><span class="pokey"></span><h3>笨蛋</h3><h3>粘土规则</h3></div>我可以使用括号,例如 (.gumby > .pokey) + h3 吗?如果没有,我的替代方案是什么?
不,括号不是 CSS 选择器中的有效运算符.它们是为函数符号保留的,例如 :lang()、:not() 和 :nth-child().p>
反正你也不需要它们;<代码>.gumby >.pokey + h3 本身就可以正常工作.
这是因为始终线性读取一系列选择器和组合器.组合器没有任何优先级.选择器可以解释为
<块引用>选择一个 h3 元素
紧跟在 pokey
类的元素之后它是类 gumby 的元素的子元素.
由于节点树的工作方式,这里使用兄弟和子组合子意味着 .pokey 和 h3 都是 .gumby,在你的情况下它们是,因为它声明它们都是兄弟姐妹.
In the below example, I want to create a CSS rule that applies only to the header with the text "Blockhead".
<div class="gumby">
<span class="pokey"></span>
<h3>Blockhead</h3>
<h3>Clay rules</h3>
</div>
Can I use parentheses, such as (.gumby > .pokey) + h3? If not, what is my alternative?
No, parentheses are not valid operators in CSS selectors. They are reserved for functional notations, such as :lang(), :not(), and :nth-child().
You don't need them anyway; .gumby > .pokey + h3 by itself will work just fine.
This is because a sequence of selectors and combinators is always read linearly. Combinators don't have any sort of precedence. The selector can be interpreted as
Select an
h3element
that immediately follows an element with classpokey
that is a child of an element with classgumby.
And because of how node trees work, the use of sibling and child combinators here implies that both .pokey and the h3 are children of .gumby, which in your case they are, because of its statement that both of them are siblings.
这篇关于CSS 选择器中是否允许使用括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS 选择器中是否允许使用括号?
基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
