CSS selector to get deepest element of specific class in the HTML tree(CSS选择器获取HTML树中特定类的最深元素)
问题描述
我的 HTML 中有一堆 DIV 元素,其中一些元素的类属性设置为rowsLayout".其中一些 rowsLayout DIV 可以相互嵌套.我想定义一个 CSS 选择器,它只针对那些嵌套中最深的 DIV.也就是说,我不想要包含任何其他 rowLayout DIV 的任何 rowsLayout DIV.
I've got a a bunch of DIV elements in my HTML, several of which have their class attribute set to "rowsLayout". Some of these rowsLayout DIVs can be nested inside one another. I want to define a CSS selector that only targets the deepest DIVs in those nestings. That is, I don't want any of the rowsLayout DIVs that contain any other rowLayout DIVs.
<div id="a" class="rowsLayout">
<div id="b" class="rowsLayout" />
<div id="c" class="rowsLayout">
<div id="d" class="rowsLayout" />
</div>
</div>
<div id="e" class="rowsLayout" />
有了这个结构,我想要一个以 b、d 和 e 为目标的选择器.
With this structure, I want a selector that will target b, d, and e.
这个可以吗?
推荐答案
可以使用jQuery选择器.rowsLayout:not(:has(.rowsLayout)).
You can use the jQuery selector .rowsLayout:not(:has(.rowsLayout)).
但是,出于性能原因,这在 CSS 中是不可能的.
However, for performance reasons, this is not possible in CSS.
您的选择器取决于您定位的元素的子元素(或缺少子元素).
CSS 的设计使得元素的选择器总是可以在元素的子元素存在之前被解析;这允许在加载文档时应用 CSS.
Your selector depends on the children (or lack thereof) of the elements that you target.
CSS is designed so that an element's selectors can always be resolved before the element's children exist; this allows CSS to be applied as a document loads.
这篇关于CSS选择器获取HTML树中特定类的最深元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS选择器获取HTML树中特定类的最深元素
基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
