jQuery:更改表格两行的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6712706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:16:28  来源:igfitidea点击:

jQuery: change the order of two rows of a table

jquery

提问by ziiweb

I want to change the order of two rows in a table.

我想更改表中两行​​的顺序。

I have this code:

我有这个代码:

console.log(position.parent().parent().prev());
console.log(position.parent().parent());
//I expected this line do the work, but no...
$(this).parent().parent().prev().insertAfter($(this).parent().parent());

That is printing this:

那就是打印这个:

<tr>?
<td>?Element 1?</td>?
<td>?…?</td>?
<td>?2008-02-02?</td>?
<td class=?"jander" data-pos=?"0" data-category=?"1">?…?</td>?
</tr>?

<tr>?
<td>?Element 2?</td>?
<td>?…?</td>?
<td>?2007-02-02?</td>?
<td class=?"jander" data-pos=?"1" data-category=?"1">?…?</td>?
</tr>?

Any idea?

任何的想法?

Regards

问候

Javi

哈维

回答by user113716

var row = $(this).closest('tr');

row.insertAfter( row.next() );

Example:http://jsfiddle.net/hkkKs/

示例:http : //jsfiddle.net/hkkKs/

Depends on which one you're targeting. If the first one has the click handler, then you'd need the code above.

取决于你的目标是哪一个。如果第一个有点击处理程序,那么您需要上面的代码。

Also, the closest()[docs]method is a safer way to target the ancestor <tr>. That may have been the issue.

此外,closest()[docs]方法是一种更安全的定位祖先的方法<tr>。这可能是问题所在。

If you want it the opposite way, your code would work, but again, use .closest()instead.

如果您想要相反的方式,您的代码可以工作,但同样,请.closest()改用。

$('span').click(function() {
    var row = $(this).closest('tr');

    row.prev().insertAfter(row);
});

Example:http://jsfiddle.net/hkkKs/1/

示例:http : //jsfiddle.net/hkkKs/1/

回答by ShankarSangoli

Try this

尝试这个

$("tableSelector").append($("tableSelector").find("tr:first"));