2009-10-12 103 views
1

我有點困惑。我需要根據他們的孩子是否擁有某個班級來選擇div標籤。這是DOM結構的代碼示例:jQuery選擇器問題

<div id="container"> 

    <div id="one"> 
     <p>This is item one</p> 
     <p class="special">This is a description</p> 
    </div> 

    <div id="two"> 
     <p>This is item one</p> 
     <p>This is a description</p> 
    </div> 

    <div id="three"> 
     <p>This is item one</p> 
     <p class="special">This is a description</p> 
    </div> 

</div> 

所以,我要選擇的是沒有帶班的「特殊」,並在上面的例子中,一個段落div標籤將是第二個div標籤(#two)。

任何想法?

回答

14

您可以使用:not:hasselectors

$('div:not(:has(p.special))') 
+0

+1毆打我給它。 – geowa4 2009-10-12 12:37:52

+0

不錯,簡潔 - 讓jQuery爲你做點修改; p – Rowan 2009-10-12 12:41:33

+0

太棒了,這個工作就像一個魅力。謝謝! – Indyber 2009-10-12 13:01:30

0

試試這個:

$("div").filter(function() { 
    return $(this).children("p.special").length == 0; 
})