2012-05-24 62 views
0

假設我有一個HTML標記是這樣的:如何使用jquery選擇器來選擇這樣的元素?

<div> 
    <p> 
     this is the parent 
     <p> 
      this the child 
     </p> 
    </p> 
    <p> 
     this is the parent 
     <p> 
      this the child 
      <p>this is third child</p> 
     </p> 
    </p> 
</div> 

在div我有五個<p>標籤,但我只想要使用jQuery選擇器選擇兩個頂級父p標籤,而不class和id名稱

這是可能的嗎?我該如何做到這一點?

回答

0

當然,只是做

$('div').children('p) 

孩子只能選擇一個元素的直接孩子。不要忘了設置一個ID到你的分區...