问题描述
似乎 CSS 是右关联的,并且与编程语言不同,您不能用括号来影响它.
我有这个一般结构:
<div class="pizza"></div></div><p>选择我!选择我!</p><div class="披萨"><p>不要选择我!</p></div></div>我无法找出一个 <p> 的选择器,该选择器跟随一个包含 <div class=" 的兄弟 <div>比萨">.
我试过这个,但 CSS 的从右到左的关联性并没有产生我想要的:
div >div.pizza + p
我知道这是不对的.
有人可以指点一下吗?
解决方案 组合器,至少是目前可用的组合器,只能表达两个元素之间的关系.正如您正确观察到的那样,您无法更改组合子的关联性.正因为如此,而且没有父元素对应于子元素的 > 组合子,所以不可能构造一个 CSS 选择器来表示 both
div >div.pizza
和
div + p
每个选择器中的第一个 div 代表相同的元素.
这个关联性问题可以使用建议的 :has() 伪类来解决,它在功能性伪类中为您提供了相对选择器语法,允许您将这样的选择器构造为
div:has(> div.pizza) + p
其中 p 是最外层选择器的主题.相对选择器 >div.pizza 是 范围 到第一个 div 选择器——本质上,这是上面前两个复杂选择器的组合,:has() 伪类的作用与任何其他简单选择器一样.
目前尚不清楚这个提议的功能是否会在 CSS 中实现.
查看我对这些相关问题的回答以了解更多信息:
- CSS 选择器中是否允许使用括号?
- 如何使用CSS根据页面中另一个元素的状态选择一个元素?
It seems CSS is right associative, and unlike programming languages, you cannot influence this with parentheses.
I have this general structure:
<div>
<div class="pizza"></div>
</div>
<p>Select me! Select me!</p>
<div>
<div class="pizza">
<p>Do NOT select me!</p>
</div>
</div>
I can't figure out the selector for a <p> that follows a sibling <div> containing a <div class="pizza">.
I tried this but the right-to-left associativity of CSS does not yield what I want:
div > div.pizza + p
I know this isn't right.
Can someone offer a pointer?
解决方案 Combinators, at least the ones that are currently available, can only express a relationship between exactly two elements. As you've correctly observed, you cannot change the associativity of combinators. Because of this, and the fact that there is no parent counterpart to the > combinator for child elements, it is not possible to construct a CSS selector that represents both
div > div.pizza
and
div + p
where the first div in each selector represents the same element.
This associativity issue can be solved using the proposed :has() pseudo-class, which provides you with a relative selector syntax within a functional pseudo-class, allowing you to construct such selectors as
div:has(> div.pizza) + p
where p is the subject of the outermost selector. The relative selector > div.pizza is scoped to the first div selector — essentially, this is a combination of both of the first two complex selectors above, with the :has() pseudo-class acting just like any other simple selector.
It is not known yet if this proposed feature will be implemented in CSS.
See my answers to these related questions for more info:
- Are parentheses allowed in CSS selectors?
- How do I select an element based on the state of another element in the page with CSS?
这篇关于邻接的 CSS 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09
前端开发问题
313
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26
前端开发问题
493
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20
前端开发问题
6
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20
前端开发问题
6
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)