2012-08-17 91 views
1

我有一張表,我想要獲取除第一個以外的所有行的HTML。jQuery如何獲取除第一個以外的所有行HTML?

<table id="files-table"> 
    <tbody> 
      <tr> 
      <th class="text-left">Name</th> 
      <th>Type</th> 
      <th class="delete-th">Delete</th> 
      </tr> 
      <tr> 
      <td class="text-left"><label class="label-none">a.docx</label></td> 
      <td><label class="label-none">Manuscript</label></td> 
      <td><input type="checkbox" class="del-cb"></td> 
      </tr> 
      <tr> 
      <td class="text-left"><label class="label-none">test2.doc</label></td> 
      <td><label class="label-none">Manuscript</label></td> 
      <td><input type="checkbox" class="del-cb"></td> 
      </tr> 
     </tbody> 
</table> 

當我使用

$('#files-table> tbody > tr').not(':first').html() 

我只得到了第2行的HTML,哪來第三的HTML?

回答

2

因爲html方法返回第一個匹配元素的html內容。

獲取匹配元素集合中第一個元素的HTML內容。

可以使用each()方法,試試這個:

$('#files-table> tbody > tr').not(':first').each(function(){ 
    console.log($(this).html()) 
}) 

Fiddle

您可以定義一個變量,並存儲匹配的元素的HTML內容:

var html = ''; 

$('#files-table> tbody > tr:not(":first")').each(function(){ 
    html += $(this).html() 
}) 
1

這是因爲你不是lo選擇每個tr對象 - 所以它只會返回第一個對象,它是第二個對象tr

$('#files-table> tbody > tr').not(':first').each(function(){ 
    console.log($(this).html()); 
}); 
相關問題