与在嵌套结构中通过.class选择last-of-type时的区别 huangapple 117266文章 0评论 2023年3月7日 06:51:30go评论137阅读模式 英文: <div> vs <nav> when selecting last-of-type by .class in a nested structure 问题 我想保持简单。这个想法是选择嵌套结构中特定类的最后一个直接子元素。为什么它适用于 <nav> 标签,但不适用于 <div>? 使用 <div> 标签: .super > .sub:last-of-type { background-color: turquoise; } <div class="super"> <div class="sub">1</div> <div class="sub">2</div> <div class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <div class="super"> <div class="sub">__1</div> <div class="sub">__2</div> </div> </div> </div> 使用 <nav> 标签: .super > .sub:last-of-type { background-color: turquoise; } <nav class="super"> <div class="sub">1</div> <div class="sub">2</div> <nav class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <nav class="super"> <div class="sub">__1</div> <div class="sub">__2</div> </nav> </nav> </nav> 所以我有两个问题: 为什么? 我能否使用 <div> 标签实现与 <nav> 标签相同的行为? 英文: I want to keep it simple. The idea is to select the last immediate child by a certain class in a nested structure. Why it works with <nav> tag but <div>? With <div> tag: <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-css --> .super > .sub:last-of-type { background-color: turquoise; } <!-- language: lang-html --> <div class="super"> <div class="sub">1</div> <div class="sub">2</div> <div class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <div class="super"> <div class="sub">__1</div> <div class="sub">__2</div> </div> </div> </div> <!-- end snippet --> With <nav> tag: <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-css --> .super > .sub:last-of-type { background-color: turquoise; } <!-- language: lang-html --> <nav class="super"> <div class="sub">1</div> <div class="sub">2</div> <nav class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <nav class="super"> <div class="sub">__1</div> <div class="sub">__2</div> </nav> </nav> </nav> <!-- end snippet --> So I have two questions here. Why? Could I even achieve the same <nav> behaviour with <div> tag? 答案1 得分: 3 我认为你感到困惑的原因是 :last-of-type 可以被理解为"标签的最后一个"而不是"选择器的最后一个"。 所以,当你使用 .sub:last-of-type 并且选择所有的 DIV 元素时,实际上是在说"最后一个同时也有 .sub 类的 div"。让我们再看一下你的代码: <div class="super"> <div class="sub">1</div> <div class="sub">2</div> <!-- 这不是最后一个 DIV --> <div class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <!-- 这不是最后一个 DIV --> <div class="super"> <div class="sub">__1</div> <div class="sub">__2</div> <!-- 这是最后一个 DIV --> </div> </div> </div> 在上面的示例中,你有两个不是最后一个 DIV 类型的 DIV 元素,尽管它们是带有 .sub 类的类型的最后一个 - 这不是 :last-of-type 选择器的目标。这很令人困惑。 当你使用 nav 时,.sub 元素确实是 "DIV" 类型的最后一个: <nav class="super"> <div class="sub">1</div> <div class="sub">2</div> <!-- 这是 DIV 类型的最后一个 --> <nav class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <!-- 这是 DIV 类型的最后一个 --> <nav class="super"> <div class="sub">__1</div> <div class="sub">__2</div> <!-- 这是 DIV 类型的最后一个 --> </nav> </nav> </nav> 更新(来自评论): 我建议使用除 DIV 以外的任何标签作为 .super 元素,就像上面的 nav 标签一样。你甚至可以创建自己的自定义元素。 问题提问者最终找到了一个仅使用 CSS 的解决方案,我将根据他们的要求发布,尽管我个人发现它很难阅读和理解。不过这是一个聪明的解决方案: .super > .sub:has(+ .super), .super > .sub:last-child { background-color: turquoise; } 英文: I think the confusion you are having is that :last-of-type can be thought of as "last of tag" rather than "last of selector". So, when you do .sub:last-of-type and you are using all DIVs, you are effectively saying "the last div which also has the class .sub". Let's look at your code again: <div class="super"> <div class="sub">1</div> <div class="sub">2</div> <!-- this is not the last DIV --> <div class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <!-- this is not the last DIV --> <div class="super"> <div class="sub">__1</div> <div class="sub">__2</div> <!-- this IS the last DIV --> </div> </div> </div> In the above example, you have two DIVs which are not the last of type DIV, even thought they are the last of type with class .sub - that's not what the :last-of-type selector targets. It's confusing. When you use nav, the .sub elements are indeed the last of type "DIV": <nav class="super"> <div class="sub">1</div> <div class="sub">2</div> <!-- this is the last of type DIV --> <nav class="super"> <div class="sub">_1</div> <div class="sub">_2</div> <!-- this is the last of type DIV --> <nav class="super"> <div class="sub">__1</div> <div class="sub">__2</div> <!-- this is the last of type DIV --> </nav> </nav> </nav> UPDATE (from comments): My suggestion would be to use any tag other than DIV for the .super elements as is done with the nav tags above. You can even create your own custom elements. The OP eventually found a CSS-only solution which I am posting on their behest, though I personally find it hard to read and understand. It is a clever solution nonetheless: .super > .sub:has(+ .super), .super > .sub:last-child { background-color: turquoise; } 通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。 点赞 https://go.coder-hub.com/75656552.html 复制链接 复制链接 本文由 huangapple 发表于 2023年3月7日 06:51:30 转载请务必保留本文链接:https://go.coder-hub.com/75656552.html css html Necessary changes required to use import {general_js} from 'general-js-toolkit' instead of import {checkIsEmpty} from 'general-js-toolkit/dist/util' go 71 05/10 可以将滚动事件的上下文设置为 `document.body` 而不是 `window` 吗? go 81 05/25 I want to simplify this javascript code. There are many children elements. go 72 04/13 How to insert a custom header into a table rendered with DT Shiny with code from a separate module? go 73 06/06 What's the correct way to type hint an empty list as a literal in python? 如何在Highcharts Gantt中更改本地化的星期名称 如何在同一个流中使用多个过滤器和映射函数? 如何使用Map/Set来将代码优化到O(n)? .NET MAUI Android在GitHub Actions上构建失败,错误代码为1。 如何在Playwright视觉比较中屏蔽多个定位器? 在C++中,可以使用可变模板参数来检索类型的内部类型。 selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found Creating and opening a URL to log in to Website via Basic Auth with Robot Framework/Selenium (Python) AG Grid 在上下文菜单中以大文本形式打开 发表评论 匿名网友 确定 昵称 邮箱 网址 Address 取消
Necessary changes required to use import {general_js} from 'general-js-toolkit' instead of import {checkIsEmpty} from 'general-js-toolkit/dist/util' go 71 05/10
How to insert a custom header into a table rendered with DT Shiny with code from a separate module? go 73 06/06