2014-01-17 32 views
1

的HTML代碼如下:jQuery的第n個孩子顯示未定義指數1

<div id="customer-projects"> 
     <h3 class="lead"> Projects </h3> 
     <ul class="project-list"> 
     . 
     . 
     </ul> 
     . 
     . 
     <ul class="project-list"> 
     . 
     . 
     </ul> 
<div> 

有什麼特別的原因$('#customer-projects .project-list:nth-child(1)').html()返回「不確定」,而不是第一UL的內容。

回答

0

修改您選擇這樣的辯論,因爲這個問題是,你正在使用nth-child(1)將匹配h3元素,而不是ul.project-list,所以它會return undefined,使用nth-of-type(1)element.class選擇。

console.log($('#customer-projects ul.project-list:nth-of-type(1)').html()); 

Demo

0

嘗試的:eq(1)代替:nth-child(1)

1

使用eq()

$('#customer-projects .project-list:eq(0)').html(); //indexing starts from 0 

Demo