Javascript 如何沿自动排列行号顺序上下排列表格行?

Javascript 如何沿自动排列行号顺序上下排列表格行?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,HTML: JavaScript: table.myTable { counter-reset: rowNumber; } table.myTable tr { counter-increment: rowNumber; } table.myTable tr td:first-child::before { content: counter(rowNumber); min-width: 1em; margin-right: 0.5e

HTML:

JavaScript:

 table.myTable {
     counter-reset: rowNumber;
 }

 table.myTable tr {
     counter-increment: rowNumber;
 }

 table.myTable tr td:first-child::before {
     content: counter(rowNumber);
     min-width: 1em;
     margin-right: 0.5em;
 }
函数MoveUp(){
var tablebody=document.getElementById('mytable');
如果(TableLocation>0){
tablebody.moveRow(TableLocation,TableLocation-1);
TableLocation=TableLocation-1;
}
}
函数MoveDown(){
var tablebody=document.getElementById('mytable');
如果((TableLocation>0)&(TableLocation
我从后端获取表数据,并从css添加行号。我使用JavaScript来上下移动行。它不起作用。如果发生上下移动行的情况,则必须将行号从一个自动排列到最后一个

这是我的名片。

您必须找到按钮的父级
tr
,然后找到要追加的元素。由于没有在一个元素后追加的函数,所以当您要向下移动时,必须找到下一个元素

上移功能:

 function MoveUp() {
     var tablebody = document.getElementById('mytable'); 
     if(TableLocation > 0) { 
         tablebody.moveRow(TableLocation, TableLocation - 1);
         TableLocation = TableLocation - 1;
     }
 }

 function MoveDown() {
     var tablebody = document.getElementById('mytable'); 
     if((TableLocation > 0) && (TableLocation < tablebody.rows.length - 1)) { 
         tablebody.moveRow(TableLocation, TableLocation + 1);
         TableLocation = TableLocation + 1;
     }
 }
function MoveUp() {
    var table,
        row = this.parentNode;

    // find the parent tr ( the row element )
    while ( row != null ) {
        if ( row.nodeName == 'TR' ) {
            break;
        }
        row = row.parentNode;
    }
    // the parent of the row is the table body
    table = row.parentNode;
    // insert the row before it's previevs siblings
    table.insertBefore ( row, get_previoussibling( row ) );
}
下移功能:

 function MoveUp() {
     var tablebody = document.getElementById('mytable'); 
     if(TableLocation > 0) { 
         tablebody.moveRow(TableLocation, TableLocation - 1);
         TableLocation = TableLocation - 1;
     }
 }

 function MoveDown() {
     var tablebody = document.getElementById('mytable'); 
     if((TableLocation > 0) && (TableLocation < tablebody.rows.length - 1)) { 
         tablebody.moveRow(TableLocation, TableLocation + 1);
         TableLocation = TableLocation + 1;
     }
 }
function MoveUp() {
    var table,
        row = this.parentNode;

    // find the parent tr ( the row element )
    while ( row != null ) {
        if ( row.nodeName == 'TR' ) {
            break;
        }
        row = row.parentNode;
    }
    // the parent of the row is the table body
    table = row.parentNode;
    // insert the row before it's previevs siblings
    table.insertBefore ( row, get_previoussibling( row ) );
}

像这样的东西正是你想要的!!!“嗨,”他们说。如果我有5列,其中有一列我不想在上下移动时更改。那么这个代码怎么可能呢?