How to select of the with javascript?
前端 未结 6 897
忘了有多久
忘了有多久 2020-12-15 22:35

I know this is very easy question, but I couldn\'t find the answer anywhere. Only answers are the ones using jQuery, not pure JS. I\'ve tried the code below and it doesn\'t

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 23:16

    var t = document.getElementById("table"),
        d = t.getElementsByTagName("tr"),
        r = d.getElementsByTagName("td");
    

    needs to be:

    var t = document.getElementById("table"),
        tableRows = t.getElementsByTagName("tr"),
        r = [], i, len, tds, j, jlen;
    
    for ( i =0, len = tableRows.length; i

    Because getElementsByTagName returns a NodeList an Array-like structure. So you need to loop through the return nodes and then populate you r like above.

提交回复
热议问题