【发布时间】:2015-06-18 15:44:54
【问题描述】:
我有以下html
<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>
如何找到没有类属性的 div 元素?即三四?
【问题讨论】:
标签: javascript jquery
我有以下html
<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>
如何找到没有类属性的 div 元素?即三四?
【问题讨论】:
标签: javascript jquery
【讨论】:
qSA()。
使用:not选择器进行过滤
$('div:not([class])');
【讨论】:
【讨论】:
另一种选择是将.not() 与Has Attribute Selector 一起使用
$('div').not('[class]')
【讨论】:
有不同的方法可以做到这一点。您可以使用 .children() 获取列表,然后对其进行索引。或者你可以查找第二个元素并使用 .next() 来获取它的兄弟。
【讨论】:
假设您不想选择所有没有类的分隔符,如果您确切知道它们在容器中的位置,则可以使用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"
【讨论】:
:contains。