javascript 使用javascript交换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8862006/
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-10-26 04:48:53  来源:igfitidea点击:

swapping rows using a javascript

javascripthtml

提问by nithin

I've a multiple number of rows which is generated by a for condition, with 2 icons up and down. On click of up the selected row should move one step up, and on click of down the selected row should move one step down

我有多个由 for 条件生成的行,上下有 2 个图标。单击向上所选行应向上移动一步,向下单击所选行应向下移动一步

<html>
<head>
<body>
<form name="myForm">

 <table id="mainTable" border="1" width="100%">

   <script>
     document.write('<tr>')
     document.write('<td>')
     document.write('row1')
     document.write('</td>')
     document.write('</tr>')

     document.write('<tr>')
     document.write('<td>')
     document.write('row2')
     document.write('</td>')
     document.write('</tr>')
     document.write('</table>')

     document.write('<table>')
     document.write('<tr>')
     document.write('<td>')
     document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
     document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
     document.write('</td>')
     document.write('</tr>')
     document.write('</table>')
</script>
 </table>

</form>
</body>
</head>
</html>
<script>
var mainTable = document.getElementById("mainTable"); 
function getRowIndex(el)
{
while( (el = el.parentNode) && el.nodeName.toLowerCase() !== 'tr' );

   if( el ) 
        alert(el.rowIndex);
        return el.rowIndex;
}

function swapRowUp(chosenRow) {
 if (chosenRow.rowIndex != 0) {
   moveRow(chosenRow, chosenRow.rowIndex-1); 
 }
} 
function swapRowDown(chosenRow) {
 if (chosenRow.rowIndex != mainTable.rows.length-1) {
   moveRow(chosenRow, chosenRow.rowIndex+1); 
 }
} 

function moveRow(targetRow, newIndex) {
if (newIndex > targetRow.rowIndex) {
   newIndex++; 
 }

 var mainTable = document.getElementById('mainTable'); 

 var theCopiedRow = mainTable.insertRow(newIndex); 


 for (var i=0; i<targetRow.cells.length; i++) {
   var oldCell = targetRow.cells[i]; 
   var newCell = document.createElement("TD"); 
   newCell.innerHTML = oldCell.innerHTML; 
   theCopiedRow.appendChild(newCell); 
   copyChildNodeValues(targetRow.cells[i], newCell);
 } 
//delete the old row 
 mainTable.deleteRow(targetRow.rowIndex); 
} 


function copyChildNodeValues(sourceNode, targetNode) {
 for (var i=0; i < sourceNode.childNodes.length; i++) {
   try{
     targetNode.childNodes[i].value = sourceNode.childNodes[i].value;
   }
   catch(e){

   }
 }
}

</script>

i'm unable to perform the swap operation, i get cellIndex as null

我无法执行交换操作,我将 cellIndex 设为 null

回答by Raynos

You can use some sensible JavaScript

你可以使用一些合理的 JavaScript

up.addEventListener("click", function moveRowUp() {
    var row = this.parentNode.parentNode,
        sibling = row.previousElementSibling,
        parent = row.parentNode;

    parent.insertBefore(row, sibling);
});

for some sensible HTML

对于一些合理的 HTML

<table>
    <tbody>
        <tr> 
            <td> 1 </td> 
            <td> filler </td> 
        </tr>
        <tr> 
            <td> 2 </td> 
            <td> <button id="up">up</button> </td> 
        </tr>
    </tbody>
</table>

Live Example

现场示例