我希望以下脚本将表行移动到表的顶部。我不知道如何引用第一行的 id,但是,由于 ID 是动态设置的,我尝试了 row.insertAfter(row.first()); ,但它使该行消失而不是移动它。有什么想法如何引用顶行吗?
$(".top").click(function(){
var row = $(this).parents("tr:first");
if ($(this).is(".top")) {
row.inserAfter(row.first());
} else {
return false;
}
});
<tbody id="sortable">
<tr id="row1">
<td><a href="" class="top">Top</a></td>
</tr>
<tr id="row2">
<td><a href="" class="top">Top</a></td>
</tr>
<tr id="row3">
<td><a href="" class="top">Top</a></td>
</tr>
</tbody>
最佳答案
This works ...
$(".top").click(function() {
var thisRow = $(this).parent().parent();
var firstRow = thisRow.parent().find("tr:first").not(thisRow);
thisRow.insertBefore(firstRow);
});
关于javascript - 引用表格的顶行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3998556/