我有以下html
<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>
如何找到没有class属性的div元素?即三和四?
答案 0 :(得分:6)
答案 1 :(得分:3)
使用:not
选择器过滤
$('div:not([class])');
答案 2 :(得分:3)
答案 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
或者,您可以使用.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()来获取它的兄弟。