使用insertBefore()在javascript中对表行重新排序

使用insertBefore()在javascript中对表行重新排序,javascript,html,dojo,Javascript,Html,Dojo,我正在尝试使用JavaScript在单击按钮时向上移动表行。下面的代码没有将行上移。insertBefore语句之后的警报语句也会显示 function MoveUp(x) { var RowLocation = x.id; var table = dojo.byId('dynamicTable'); var rows = table.rows; if ((RowLocation > 0) && (RowLocation < (ro

我正在尝试使用JavaScript在单击按钮时向上移动表行。下面的代码没有将行上移。
insertBefore
语句之后的警报语句也会显示

function MoveUp(x) {

    var RowLocation = x.id;
    var table = dojo.byId('dynamicTable');
    var rows = table.rows;

    if ((RowLocation > 0) && (RowLocation < (rows.length - 1))) {
        alert('inside if..'+ RowLocation);
        rows.insertBefore(rows[RowLocation],rows[RowLocation-1]);           
        alert('after shift..');
    }
}
函数上移(x){
var RowLocation=x.id;
var table=dojo.byId('dynamicTable');
var rows=table.rows;
如果((RowLocation>0)和&(RowLocation<(rows.length-1))){
警报('内部如果..'+行位置);
rows.insertBefore(rows[RowLocation],rows[RowLocation-1]);
警惕(“轮班后……”;
}
}

有人能帮忙找出错误吗?

您应该在新插入节点的父节点上调用
insertBefore
(而不是
table.rows
):

使用以下命令向下移动行:

var parentNode = rows[RowLocation].parentNode;
parentNode.insertBefore(rows[RowLocation], rows[RowLocation + 1].nextSibling)

杰出的那一个奏效了!!包含.parentNode使情况有所不同。谢谢。。!我试图用类似的逻辑将行向下移动。但它不是虫子。您可以提供帮助吗?行[RowLocation].parentNode.insertAfter(行[RowLocation],行[RowLocation+1])<代码>行[RowLocation].parentNode.insertBefore(行[RowLocation],行[RowLocation+1].nextSibling)行[RowLocation].parentNode.insertBefore(行[RowLocation],行[RowLocation].nextSibling);这个成功了。但是上面的(Rowlocation+1)不起作用。有什么想法吗?对。在小提琴上它很好用。不管怎样,我会查一查的。非常感谢你的帮助,伙计。你能给我们看看HTML代码吗?
var insertedElement = parentElement.insertBefore(newElement, referenceElement);
var parentNode = rows[RowLocation].parentNode;
parentNode.insertBefore(rows[RowLocation], rows[RowLocation + 1].nextSibling)