jquery选择器和兄弟姐妹

时间:2014-01-06 15:27:07

标签: jquery

假设我有以下设置:

<div>
 <div class="pr-selec">some text</div>
 ...
</div>


<div>
 <div class="pr-selec">some text</div>
 <div class="new-class">other text</div>
 ...
</div>

使用jQuery,如何只选择没有sibbiling .pr-selec div的.new-class div类?

我正在尝试这样的事情:

jQuery(".pr-selec").not().sibblings("new-class").each({
            //stuff
        });

但是没有这样做。

2 个答案:

答案 0 :(得分:4)

使用.not():has-selector

尝试next-sibling selector
$('.pr-selec').not(':has(~ .new-class)').css('color', 'red')

演示:Fiddle

注意:如果new-class元素不是下一个兄弟(上一个),则此功能无效:Fiddle

答案 1 :(得分:3)

您最好使用filter

jQuery(".pr-selec").filter(function(){
    return !$(this).siblings('.new-class').length;
}).each(...)