简体   繁体   English

如何使用Javascript将单元格数据值从HTML表格的一列移到另一列?

[英]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.

相关问题 HTML / JS:链接一个表的一列的单元格值与另一表的另一列的单元格的链接 - Html/JS: Link cell values of one column of one table from cells of another column of another table 将焦点从表格的一列移动到另一列时,如何调用 javascript function - how can I call javascript function when move the focus from one column to another column of the table Javascript:在HTML表格中,如何从一列中选择文本的一部分(与某些正则表达式匹配),然后将其移至下一列? - Javascript: in an HTML table, how to select part of text (matching some regex) from one column, and move it to the next one? 使用Javascript将单元格数据从一个表拉到另一个表 - Using Javascript to pull cell data from one table to another 单击时如何将图像从一个表的单元格移动到另一张表的单元格 - How to move image from one table's cell to another table's cell when clicked 如何将表行数据(HTML)从一个php页移动到另一个 - how to move the table row data (HTML) from one php page to another HTML / JS从另一个表单元格更新一个表单元格 - HTML/JS Update one table cell from another table cell 表列Javascript中的单元格数据 - Cell data from table column Javascript 使用javascript或jquery将数据从一个表移动和删除到另一个表 - Move and delete data from one table to another table using javascript or jquery 列的值从一个表转移到另一个表 - Transfer Values of a Column from One Table to Another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM