DOM遍历table

    xiaoxiao2021-04-03  39

    <!DOCTYPE html> <html> <head> <title>123</title> </head> <body> <table id="table01"> <tbody> <tr> <td>John</td> <td>Doe</td> <td>Alaska</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> </tr> </tbody> </table> <button οnclick="fun01()">成功的查询</button> <button οnclick="fun02()">失败的查询</button> <script> function fun01() { //成功的 var table = document.getElementById("table01"); var tbody = table.getElementsByTagName('tbody')[0]; var trs = tbody.getElementsByTagName("tr"); for ( var i = 0; i < trs.length; i++) { var tr = trs[i]; var tds = tr.getElementsByTagName("td"); for ( var j = 0; j < tds.length; j++) { var td = tds[j]; console.log("i:" + i + " j: " + j + " " + td.innerHTML); } } } function fun02() { //失败的 var table = document.getElementById("table01"); var tbodys = table.childNodes; console.log(tbodys); var tbody = tbodys[1]; var trs = tbody.childNodes; console.log(trs); var tds = trs[1].childNodes; console.log(tds); } </script> </body> </html>
    转载请注明原文地址: https://ju.6miu.com/read-666028.html

    最新回复(0)