Sample Code for Javascript traversal of elements in a table

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.