2013-03-21 40 views
0

如果我有以下的HTML,我怎麼使用jQuery,只選擇第一級子和適用:不給他們如何選擇的所有直接子:沒有()

<div id="top"> 
    <span>select me</span> 
    <a>select me</a> 
    <div> 
     <span>not this</span> 
     <a>not this</a> 
    </div> 
    <div class="not-this">not this</div> 
</div> 

的jQuery:

jQuery('#top>:not(.not-this)'); // errors due to the > 
jQuery('#top:not(.not-this)'); // selects the second level children. 

它需要是不可知的兒童使用的標籤。

編輯補充:還試圖

jQuery('#top>*:not(.not-this)'); // selects the second level children. 
+0

你用'>'得到什麼的jQuery的版本,什麼 「錯誤」?你聲稱的結果沒有一個應該像你說的那樣工作。 – 2013-03-21 15:26:16

+0

@JamesMontagne我剛剛重試,並沒有錯誤,但它仍然沒有做我想做的。 – SystemicPlural 2013-03-21 15:31:51

回答

2

jQuery('#top>:not(.not-this)')實際上應該只是罰款。 http://jsfiddle.net/Entxc/

另一種方法是這樣的:

$("#top").children().not(".not-this") 

http://jsfiddle.net/Entxc/1/

+0

你的小提琴選擇孩子,我不想要它。 – SystemicPlural 2013-03-21 15:31:14

+0

你特別說你想讓它選擇一級的孩子。這就是這樣做的。 – 2013-03-21 15:32:03

+0

問題是它也選擇了二級孩子 - 他們有邊界。我只想要一級的孩子。 – SystemicPlural 2013-03-21 15:32:59