我试图用 jQuery 选择表中除第一行(列标题)之外的所有行,为此我有这个选择器:
$("#tableID tr:not(:first)")
有三行,一行用于列标题,两行用于内容,选择器返回 4,但如果我这样做:
$("#tableID tr")
它返回三行就好了。我在选择器中遗漏了什么吗?
如果有帮助,我做了截图:
给我问题的代码是这样的(太简单了,我不明白为什么它不起作用)
function addTableColumn() {
var uls = $('#debate ul').length;
$('#debate tr:first ').append("<th id='foo'>foo title</th>");
$("#debate tr:not(#debate tr:first)").append("<td><ul id='sortable" + (uls+1) + "' class='connectedSortable'></ul></td>").hide().fadeIn("slow");
alert("I'm seeing " + $('#debate tr:not(:first)').length + " rows (without first row), type: " + $('#debate tr:not(:first)')[0] + " but the row count returns (including titles) " + $('#debate tr').length);
makeSortable();
}
最佳答案
如果是标题行,我建议您将标记更改为如下内容:
<table>
<thead>
<tr>
<th>Title</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
</tbody>
</table>
然后,在您的 CSS/jQuery 中,只选择 <tr>
在<tbody>
, 这样也更语义化。
关于javascript - jQuery 选择器返回重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7202283/