如何在Jquery中找到没有类的元素?

时间:2013-05-09 10:32:42

标签: javascript jquery

我有以下html

<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>

如何找到没有class属性的div元素?即三和四?

6 个答案:

答案 0 :(得分:6)

您可以使用:not选择器

$('div:not([class])');

这里是API

simple Fiddle

答案 1 :(得分:3)

使用:not选择器过滤

$('div:not([class])');

答案 2 :(得分:3)

:not()选择器与属性存在选择器[class]结合使用。

$("div:not([class])")

jsFiddle

答案 3 :(得分:1)

另一种选择是将.not()Has Attribute Selector

一起使用
$('div').not('[class]')

答案 4 :(得分:0)

假设您不想选择所有没有类的分隔符,如果您确切知道它们在容器中的位置,则可以使用nth-child来选择特定的分隔符。这将选择您的无类别分隔符:

$('div:nth-child(3)') // Selects the third divider
$('div:nth-child(4)') // Selects the fourth divider
$('div:nth-child(3), div:nth-child(4)') // Selects both

JSFiddle example

或者,您可以使用.prev().next()

进行选择
$('div.two').next() // Selects the divider after div class="two"
$('div.three').prev() // Selects the divider before div class="three"

答案 5 :(得分:0)

有不同的方法可以做到这一点。您可以使用.children()来获取列表,然后将其编入索引。或者你可以查找第二个元素并使用.next()来获取它的兄弟。