Getting value from table cell in JavaScript…not jQuery

后端 未结 12 1143
后悔当初
后悔当初 2020-12-02 18:19

I can\'t believe how long this has taken me but I can\'t seem to figure out how to extract a cell value from an HTML table as I iterate through the table with JavaScript. I

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 19:16

    I know this is like years old post but since there is no selected answer I hope this answer may give you what you are expecting...

    if(document.getElementsByTagName){  
        var table = document.getElementById('table className');
        for (var i = 0, row; row = table.rows[i]; i++) {
        //rows would be accessed using the "row" variable assigned in the for loop
         for (var j = 0, col; col = row.cells[j]; j++) {
         //columns would be accessed using the "col" variable assigned in the for loop
            alert('col html>>'+col.innerHTML);    //Will give you the html content of the td
            alert('col>>'+col.innerText);    //Will give you the td value
            }
          }
        }
    }
    

提交回复
热议问题