TableRow rowIndex 属性
JavaScript基础 2022-06-08 17:19:3216小码哥的IT人生shichen
TableRow rowIndex 属性
实例
例子 1
单击不同的行,以输出其索引位置:
alert("Row index is: " + x.rowIndex);
完整实例:
<!DOCTYPE html> <html> <head> <style> table, td { border: 1px solid black; } </style> </head> <body> <p>单击每个 tr 元素可提示其在表中的索引位置:</p> <table> <tr onclick="myFunction(this)"> <td>点击可显示 rowIndex</td> </tr> <tr onclick="myFunction(this)"> <td>点击可显示 rowIndex</td> </tr> <tr onclick="myFunction(this)"> <td>点击可显示 rowIndex</td> </tr> </table> <script> function myFunction(x) { alert("行索引是:" + x.rowIndex); } </script> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
例子 2
返回表中每一行的索引位置:
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].rowIndex+"<br>"; }
完整实例:
<!DOCTYPE html> <html> <head> <style> table, td { border: 1px solid black; } </style> </head> <body> <table> <tr> <td>行 1</td> </tr> <tr> <td>行 2</td> </tr> <tr> <td>行 3</td> </tr> </table> <p>单击该按钮可返回表中每一行的位置。</p> <p id="demo"></p> <button onclick="myFunction()">试一试</button> <script> function myFunction() { var x = document.getElementsByTagName("tr"); var txt = ""; var i; for (i = 0; i < x.length;i++) { txt = txt + "行 "+(i+1)+" 的索引是:"+x[i].rowIndex+"<br>"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
语法
返回 rowIndex 属性:
tablerowObject.rowIndex
技术细节
返回值: | 数值,表示行在表的 rows 集合 中的位置。 |
---|
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |