Sample Code for Javascript traversal of elements in a table
This article mainly introduces how Javascript traverses the elements in the table. For more information, see
For example:
<table id=tb> <tr><th> </th><th> </th><th> </th><th> </th></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr></table>
In JS, how does one traverse the value assignment?
Program code
var count=0;for(i=0; i < document.getElementById("tb1").rows.length; i++){ for(j=0; j < document.getElementById("tb1").rows(i).cells.length; j++) { document.all.getElementById("tb1").rows(i).cells(j).innerText = count; count++; }}
See another way to use dom (but I did not try it out, I don't know why)
<Table id = "mylist"> <tr> <td> A monk has water to drink. </Td> </tr> <td> two monks pick up the water. </Td> </tr> <td> three monks have no water to drink. </Td> </tr> </table> <script> var msg = "var mylist = document. getElementByIdx ("mylist") for (I = 0; I <mylist. childNodes. length; I ++) {var tr = mylist. childNodes [I]; for (j = 0; j <tr. childNodes [j]. length; j ++) {var td = tr. childNodes [j]; msg + = td. innerText ;}} alert (msg); </script>