2009-08-02 49 views

回答

27

對於:

<ul> 
    <li>Item 1</li> 
    <li>Item 2 
    <ul> 
     <li>Item 2.1</li> 
     <li>Item 2.2</li> 
    </ul> 
    </li> 
    <li>Item 3</li> 
</ul> 

例如

$("ul > li").addClass("blah"); 

增加了類 「等等」 爲1 2和3,而:

$("ul li").addClass("blah"); 

加載類 「等等」 的每一個列表元素。我不確定你指的是<和?運營商。

+3

是否將ul> li設置爲「blah」?因爲你有一個子列表,裏面也有一個孩子。 – 2009-08-12 17:04:24

10

在CSS中,>的意思是「直接子女」:只選擇直接子女的節點。

雖然空間意味着「任何後裔」:可以選擇這些孩子的直接子女和孩子。

我會打賭jQuery使用相同的約定。

2

如前所述,空格將選擇任何後代,而>將只選擇直接的子女。如果你想只選擇孫子或曾孫,那麼你可以使用這個:

#foo > * > * > .bar 

(所有帶班「吧」,這是id爲「foo」的元素的曾孫元素)

2

看看這個..

$(".testit > a") //match the first <a> tag below 
$(".testit a") // matches all <a> tag below 

<p class="testit"> 
    <a href="#">All the rules will match this</a> 
    <span> 
    <a href="#">second rule can only select this</a> 
    </span> 
</p> 
相關問題