没有任何类的元素是否有 CSS 选择器?

Is there a CSS selector for element without any class?(没有任何类的元素是否有 CSS 选择器?)
本文介绍了没有任何类的元素是否有 CSS 选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

没有任何类的元素是否有 CSS 选择器?例如在 HTML 中

Is there a CSS selector for element without any class? For example in HTML

<section>Section A</section>
<section class="special">Section B</section>
<section class="">Section C</section>

我想选择 A 部分(或者可能是 A 部分和 C 部分,没那么重要),可以这样说

I would like to select Section A (or maybe Section A and Section C, it does not matter that much), by saying something like

section:not(.*) { color: gray } 

我知道我可以将其定义为 section 并在所有特定类中重新设置它,例如在

I understand that I could define it to section and reset it back in all particular classes, like in

section { color: gray } 
section.special { color: black } 

但这不是我想要的,因为一旦样式变得复杂,它就不太容易管理,并且在某些情况下很难正确地重置"(当然不是在这个简化的示例中).

but this is not what I want, because it is not very manageable once the styles get complex and in some cases it is hard to do the "reset" properly (of course not in this simplified example).

推荐答案

使用 section:not([class]) 您可以选择没有 class 属性的每个部分.不幸的是,它不会选择具有空类属性值的那些部分.所以除此之外,我们必须排除这些部分:

With section:not([class]) you select every section without the class attribute. Unfortunately, it won't select those sections with an empty class attribute value. So in addition, we have to exclude these sections:

section:not([class]) { /* every section without class - but won't select Section C */
  color: red;
}

section[class=""] { /* selects only Section C */
  font-weight: bold;
}

<section>Section A</section>
<section class="special">Section B</section>
<section class="">Section C</section>

  • CSS 属性选择器, 浏览器支持
  • :not

这篇关于没有任何类的元素是否有 CSS 选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)
Discord js reaction not detected(未检测到 Discord js 反应)
Joining a voice channel on ready (discord.js)(准备好加入语音频道(discord.js))
Cannot read properties of undefined when it shouldn#39;t be undefined in discord.js command handler(当不应该在 discord.js 命令处理程序中未定义时,无法读取未定义的属性)
Why messageReactionAdd do nothing discord.js(为什么 messageReactionAdd 什么都不做 discord.js)
How do I list all Members with a Role In Discord.Js(如何在 Discord.Js 中列出所有具有角色的成员)