HTML DOM id 属性
JavaScript基础 2022-06-08 17:13:4116小码哥的IT人生shichen
HTML DOM id 属性
定义和用法
idr 属性可设置或返回表格的 id。
语法
tableObject.id=id
实例
<html> <head> <script type="text/javascript"> function alertId() { alert(document.getElementById("table1").id
); } </script> </head> <body> <table id="table1" border="1"> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> </tr> </table> <br /> <input type="button" onclick=alertId() value="Alert id of table" /> </body> </html>
TIY
完整实例【提示一个表格的 id】:
<html> <head> <script type="text/javascript"> function alertId() { alert(document.getElementById("table1").id); } </script> </head> <body> <table id="table1" border="1"> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> </tr> </table> <br /> <input type="button" onclick=alertId() value="Alert id of table" /> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html