2012-08-17 94 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()); 
}); 
相关问题