Javascript 如何显示HTML表中的一行并切换到下一行?

Javascript 如何显示HTML表中的一行并切换到下一行?,javascript,html-table,Javascript,Html Table,我有一个2列5行的HTML表格。您可以这样做: function findMyRow(el)(){ if(!el.parentNode) return null; else if(el.parentNode.tagName.toLowerCase()=="tr") return el.parentNode; else return findMyRow(el.parentNode); } for(var i=0;i<tbl.rows.length;i++){

我有一个2列5行的HTML表格。

您可以这样做:

function findMyRow(el)(){
    if(!el.parentNode) return null;
    else if(el.parentNode.tagName.toLowerCase()=="tr") return el.parentNode;
    else return findMyRow(el.parentNode);
}
for(var i=0;i<tbl.rows.length;i++){
    tbl.rows[i].onclick = function(e){
        var selectedRow = null;
        if(e.target.tagName.toLowerCase()=="tr") selectedRow = e.target;
        selectedRow = findMyRow(e.target);
        //this is just for preventing false event raising
        if(!selectedRow) return false;
        var rowIndex = Array.prototype.indexOf.call(tbl.rows, selectedRow);
        for(car j=0;j<selectedRow.cells.length;j++){
            var cell = selectedRow.cells[j];
            //let's say your inputs each has an unique ID based on your columns index
            //like <input id="cellData0" ttype="text" />
            //or <input id="cellData1" ttype="text" /> and so on
            document.getElementById("cellData" + j).value = cell.innerText;
        }
    }
}
函数findMyRow(el)(){
如果(!el.parentNode)返回null;
else if(el.parentNode.tagName.toLowerCase()=“tr”)返回el.parentNode;
否则返回findMyRow(el.parentNode);
}

对于(var i=0;i,使用Jquery可以执行以下操作:

$(document).ready(function(){
    window.rowIndex = 0; // gobal variable

    function displayRow(){
        var tableRows = $('#mytable tr');
        if(tableRows.length <= window.rowIndex) {
            window.rowIndex = 0;
        }
        var rowCells = $(tableRows[window.rowIndex]).find('td');
        $('.cellone').val($(rowCells[0]).html());
        $('.celltwo').val($(rowCells[1]).html());
        window.rowIndex = window.rowIndex + 1;
    };

    $('.next').click(function(event){ displayRow(); });
    displayRow();
});
$(文档).ready(函数(){
window.rowIndex=0;//gobal变量
函数displayRow(){
var tableRows=$(“#mytable tr”);
if(tableRows.length