编辑:我刚刚注意到我正在尝试交换整数,因为我正在获取 rowIndex。那不是我想做的。我想交换对象。但是,我真的不知道该怎么做。
我想通过分别交换上面的元素和下面的元素来在队列中上下移动表元素。但是,我的代码不起作用。以下是downButton的JS函数。
var downButton = document.getElementsByClassName('downButton')[0];
downButton.onclick = function moveDown(currentRow) {
var index = currentRow.parentNode.parentNode.rowIndex;
var nextRow = currentRow.nextElementSibling.parentNode.parentNode.rowIndex;
var temp = index;
index = nextRow;
nextRow = temp;
}
这是向下按钮的 html:
<button class = "downButton">down</button>
这是该表的 JSFiddle 链接:https://jsfiddle.net/Led4b3nw/
最佳答案
交换innerHTML而不是索引:
var index = currentRow.parentNode.parentNode;
var nextRow = currentRow.nextElementSibling.parentNode.parentNode;
var temp = index.innerHTML;
index.innerHTML = nextRow.innerHTML;
nextRow.innerHTML = temp;
关于javascript - 在javascript中交换html表格元素(完整对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40399800/