【发布时间】:2018-09-26 02:37:42
【问题描述】:
我正在尝试制作一个 html 表格,用户可以通过上下箭头对其中的行进行排序。但有一个字段我希望保持不变,即 id 字段。
HTML代码:
<table class="table routes_t">
<thead>
<tr>
<th>#</th>
<th>Local</th>
<th>Coordenadas</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="ord">1</td>
<td>Template2</td>
<td>37.13318,-8.5392</td>
<td>
<span class="id" data-id="9"></span>
<a href="#" onclick="remove(9)" "=""><i class="glyphicon glyphicon-remove pull-right"></i></a>
<a href="#" class="uporder"><i class="glyphicon glyphicon-arrow-down pull-right"></i></a>
<a href="#" class="downorder"><i class="glyphicon glyphicon-arrow-up pull-right"></i></a>
</td>
</tr>
<tr>
<td class="ord">2</td>
<td>Teste_unserial</td>
<td>37.13008,-8.54247</td>
<td>
<span class="id" data-id="10"></span>
<a href="#" onclick="remove(10)" "=""><i class="glyphicon glyphicon-remove pull-right"></i></a>
<a href="#" class="uporder"><i class="glyphicon glyphicon-arrow-down pull-right"></i></a>
<a href="#" class="downorder"><i class="glyphicon glyphicon-arrow-up pull-right"></i></a>
</td>
</tr>
</tbody>
</table>
我的 jQuery 是:
$(document).ready(function(){
$(".uporder,.downorder").click(function(){
var row = $(this).parents("tr:first");
if ($(this).is(".downorder")) {
row.children('td:not(:first)').insertBefore(row.prev());
} else {
row.insertAfter(row.next());
}
});
});
现在发生的情况是我想要不触及的字段未触及,但其他字段移动到不需要的位置,例如 <tbody> 的上方。
【问题讨论】:
标签: jquery html-table