Javascript traverses the sample code for the elements in the table
- 2020-03-30 03:29:37
- OfStack
Such as:
<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>
How do we iterate through assignments in JS?
The 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++;
}
}
I saw another way to use dom (but I didn't try it myself, I don't know why).
<table id="mylist">
<tr><td> And he had water to drink. </td></tr>
<tr><td> Two boys are a boy. </td></tr>
<tr><td> The three monks had 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>