Edit This Code:
See Result »
<!DOCTYPE html> <html> <head> <style> table, td { border: 1px solid black; } </style> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?73c27e26f610eb3c9f3feb0c75b03925"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </head> <body> <table> <thead> <tr> <th>Row 1</th> </tr> <tr> <th>Row 2</th> </tr> </thead> <tbody> <tr> <td>Row 3</td> </tr> <tr> <td>Row 4</td> </tr> </tbody> <tfoot> <tr> <td>Row 5</td> </tr> <tr> <td>Row 6</td> </tr> </tfoot> </table> <p>Click the button to return the position of the rows in the thead, tbody, and tfoot rows collection.</p> <p id="demo"></p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var x = document.getElementsByTagName("tr"); var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + "The index of Row "+(i+1)+" is: "+x[i].sectionRowIndex+"<br>"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
Result: