Home >Web Front-end >JS Tutorial >Javascript traverses elements in table sample code_javascript skills

Javascript traverses elements in table sample code_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:42:121113browse

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>

How to traverse assignment in JS?
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>一个和尚有水喝。</td></tr>
<tr><td>两个和尚挑水喝。</td></tr>
<tr><td>三个和尚没水喝。</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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn