JavaScript onClick event - HTML table

后端 未结 3 1186
抹茶落季
抹茶落季 2021-02-05 11:30

I\'m learning JavaScript and recently I have been experimenting with Mouse events, trying to understand how they work.



    

        
3条回答
  •  后悔当初
    2021-02-05 12:02

    var table = document.getElementById("tableID");
    if (table != null) {
        for (var i = 0; i < table.rows.length; i++) {
            for (var j = 0; j < table.rows[i].cells.length; j++)
            table.rows[i].cells[j].onclick = function () {
                tableText(this);
            };
        }
    }
    
    function tableText(tableCell) {
        alert(tableCell.innerHTML);
    }
    

    is an example of what you could do. DEMO

提交回复
热议问题