选择没有任何类的元素
我需要通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
:not()
和 属性非选择器[att!=val]
过滤掉带有非-的元素空class
属性:jsFiddle 预览
但请注意
[att !=val]
是一个非标准 jQuery 选择器,这意味着该选择器不能在 CSS 中使用,也不能与document.querySelectorAll()
一起使用。如果您像我一样,并且坚持遵循标准,因此希望尽可能避免非标准 jQuery 选择器,则以下是直接等效项:这与
span
元素 匹配: 没有class
属性,以及class
属性的span
元素,但是仅当属性值为空时。不过,在大多数情况下,您应该能够只使用第一部分:
您通常只会在负责输出它的应用程序或开发人员生成的标记中找到空的
class
属性谁不注意。Use
:not()
and the attribute not selector[att!=val]
to filter out elements with a non-emptyclass
attribute:jsFiddle preview
Note however that
[att!=val]
is a non-standard jQuery selector, which means the selector cannot be used in CSS or withdocument.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:This matches
span
elements that have noclass
attribute, andspan
elements that do have aclass
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:
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.请参阅此答案仅使用CSS 我可以编写一个 CSS 选择器来选择不具有特定类的元素吗?
实际上,这将选择任何没有应用 .class 的元素。
我收集了 jsfiddle 演示
html
css
See this answer to do it with css only Can I write a CSS selector selecting elements NOT having a certain class?
Actually, this will select anything witch do not have a .class applied to it.
I gathered a jsfiddle demo
html
css