[英]How to move cell data values from one column to another in HTML table with Javascript?
I want to rotate the values in HTML table rows with button, but in the following code "Move" button is not working. 我想使用按钮旋转HTML表格行中的值,但是在以下代码中,“移动”按钮不起作用。 Display button will give following table: 显示按钮将给出下表:
0 1 2 0 1 2
0 1 2 0 1 2
0 1 2 0 1 2
by clicking on move button I want table as follows: 通过单击移动按钮,我想要表格如下:
2 0 1 2 0 1
2 0 1 2 0 1
2 0 1 2 0 1
and keep rotating the values in row by clicking the move button 并通过单击移动按钮继续旋转值
<body>
<script>
var myArray = new Array();
var i=0;
var j=0;
function display()
{
var table = document.createElement('table');
table.setAttribute("id", "tbl");
for (i = 0; i < 4; i++){
var tr = document.createElement('tr');
for (j = 0; j < 4; j++)
{
var td= document.createElement('td');
var text = document.createTextNode(j);
td.appendChild(text);
tr.appendChild(td);
table.appendChild(tr);
document.body.appendChild(table);
}
}
}
function move()
{
var table = document.getElementById('tbl');
for(i=0;i<4;i++)
{
var x = table.rows[i].cells[9].innerHTML;
for(j=0;j<4;j++)
{
table.rows[i].cells[j+1].innerHTML = table.rows[i].cells[j].innerHTML;
}
table.rows[i].cells[0].innerHTML=x;
}
}
</script>
HTML: HTML:
<input id="display" type="button" value="Display" onclick="display();" />
<input id="move" type="button" value="Move" onclick="move();" />
maybe: 也许:
function display() { var table = document.createElement('table'); table.setAttribute("id", "tbl"); for (var i = 0; i < 4; i++) { var tr = document.createElement('tr'); for (var j = 0; j < 4; j++) { var td = document.createElement('td'); var text = document.createTextNode(j); td.appendChild(text); tr.appendChild(td); } table.appendChild(tr); } document.body.appendChild(table); } function move() { var table = document.querySelector('#tbl'); var tr = table.rows; for (var i = 0; i < tr.length; i++) { tr[i].insertBefore(tr[i].lastElementChild, tr[i].firstElementChild); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.