选择没有任何类的元素

发布于 2024-10-17 07:22:58 字数 180 浏览 0 评论 0原文

我需要通过 jQuery 选择器找到页面中没有类的所有跨度。

例子:

<span class='Cool'>do not found me</span>
<span>me, me, take me please!!!</span>

I need to find, via jQuery selectors, all spans in a page that have no class.

Example:

<span class='Cool'>do not found me</span>
<span>me, me, take me please!!!</span>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南…巷孤猫 2024-10-24 07:22:58

使用 :not()属性非选择器[att!=val]过滤掉带有非-的元素空 class 属性:

$('span:not([class!=""])')

jsFiddle 预览

但请注意 [att !=val] 是一个非标准 jQuery 选择器,这意味着该选择器不能在 CSS 中使用,也不能与 document.querySelectorAll() 一起使用。如果您像我一样,并且坚持遵循标准,因此希望尽可能避免非标准 jQuery 选择器,则以下是直接等效项:

$('span:not([class]), span[class=""]')

这与

  • 具有以下内容的 span 元素 匹配: 没有 class 属性,以及
  • 确实class 属性的 span 元素,但是仅当属性值为空时。

不过,在大多数情况下,您应该能够只使用第一部分:

$('span:not([class])')

您通常只会在负责输出它的应用程序或开发人员生成的标记中找到空的 class 属性谁不注意。

Use :not() and the attribute not selector [att!=val] to filter out elements with a non-empty class attribute:

$('span:not([class!=""])')

jsFiddle preview

Note however that [att!=val] is a non-standard jQuery selector, which means the selector cannot be used in CSS or with document.querySelectorAll(). If you're like me, and you're a stickler for following the standards and so want to eschew non-standard jQuery selectors where possible, the following is a direct equivalent:

$('span:not([class]), span[class=""]')

This matches

  • span elements that have no class attribute, and
  • span elements that do have a class attribute, but only when the attribute value is empty.

In most cases, though, you should be able to get away with just the first portion:

$('span:not([class])')

You'll usually only find empty class attributes in markup generated by the application that's responsible for outputting it, or by developers who aren't paying attention.

得不到的就毁灭 2024-10-24 07:22:58

请参阅此答案仅使用CSS 我可以编写一个 CSS 选择器来选择不具有特定类的元素吗?

:not([class])

实际上,这将选择任何没有应用 .class 的元素。

我收集了 jsfiddle 演示

html

<h2 class="fake-class">fake-class will be green</h2>
<h2 class="">empty class SHOULD be white</h2>
<h2>no class should be red</h2>
<h2 class="fake-clas2s">fake-class2 SHOULD be white</h2>
<h2 class="">empty class2 SHOULD be white</h2>
<h2>no class2 SHOULD be red</h2>

css

h2 {color:#fff}
:not([class]) {color:red;background-color:blue}
.fake-class {color:green}

See this answer to do it with css only Can I write a CSS selector selecting elements NOT having a certain class?

:not([class])

Actually, this will select anything witch do not have a .class applied to it.

I gathered a jsfiddle demo

html

<h2 class="fake-class">fake-class will be green</h2>
<h2 class="">empty class SHOULD be white</h2>
<h2>no class should be red</h2>
<h2 class="fake-clas2s">fake-class2 SHOULD be white</h2>
<h2 class="">empty class2 SHOULD be white</h2>
<h2>no class2 SHOULD be red</h2>

css

h2 {color:#fff}
:not([class]) {color:red;background-color:blue}
.fake-class {color:green}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文