上下移动表行 - Jquery/Javascript

Rav*_*avi 1 javascript html-table

任何人都可以告诉我如何通过jQuery/Javascript上下移动表行.

我有一张桌子,每排第一个td都有一个单选按钮.单击向上/向下箭头时,所选行应向上/向下移动.

期待一些想法......

Sto*_*ive 6

首先获取所选行:

var radio;
// assuming there's only one form in your page, replace 0 with whatever it is
// and inputs have name 'radioGroupName'
for (var i in document.forms[0].radioGroupName) {
    if (documents.forms[0].radioGroupName[i].checked) {
        radio = documents.forms[0].radioGroupName[i].parentNode.parentNode;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

向上移动:

var prev = radio.previousSibling;
var par = radio.parentNode;
if (prev) {
    par.removeChild(radio);
    par.insertBefore(radio, prev);
}
Run Code Online (Sandbox Code Playgroud)

向下移动:

var next = radio.nextSibling;
var par = radio.parentNode;
par.removeChild(radio);
if (next.nextSibling)
    par.insertBefore(radio, next.nextSibling);
else
    par.appendChild(radio);
Run Code Online (Sandbox Code Playgroud)