How do I iterate through table rows and cells in JavaScript?

前端 未结 9 1068
旧时难觅i
旧时难觅i 2020-11-22 13:05

If I have an HTML table...say

<
9条回答
  •  误落风尘
    2020-11-22 13:35

    Try

    for (let row of mytab1.rows) 
    {
        for(let cell of row.cells) 
        {
           let val = cell.innerText; // your code below
        }
    }
    

    for (let row of mytab1.rows) 
    {
        for(let cell of row.cells) 
        {
           console.log(cell.innerText)
        }
    }
col1 Val1
col1 Val1 col2 Val2
col1 Val3 col2 Val4

for ( let [i,row] of [...mytab1.rows].entries() ) 
{
    for( let [j,cell] of [...row.cells].entries() ) 
    {
       console.log(`[${i},${j}] = ${cell.innerText}`)
    }
}
col1 Val1 col2 Val2
col1 Val3 col2 Val4

提交回复
热议问题