选择没有任何类的元素 [英] Select elements without any class

查看:87
本文介绍了选择没有任何类的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


$ b 示例:

 < span class ='Cool'>找不到我< / span> 
< span>我,我,请带我!!!< / span>


解决方案

使用 :not() 属性不选择 [att!= val] 来过滤掉非-empty class 属性:

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

的jsfiddle预览



注意然而该 [ATT!= VAL] 是一个非标准的jQuery选择器,这意味着选择器不能用于CSS或 document.querySelectorAll()。如果你和我一样,并且你是遵守标准的人,并且希望在可能的情况下避开非标准的jQuery选择器,下面是一个直接的等价物:



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

匹配


  • span 元素有 no class 属性和

  • span 元素具有属性,但仅当属性值为空时

>

然而,在大多数情况下,您应该能够避开第一部分:

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

通常只在负责输出它的应用程序生成的标记中找到空的属性,或者由不注意的开发者找到。

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>

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆