语法:
document.getElementById(“id”)
结果:null或[object HTMLParagraphElement].
除了 getElementById() 之外,还有 getElementsByName() 和 getElementsByTagName()。
不过,最有效的方法是 getElementById()。
实例:
<html> <head> <script type="text/javascript"> function getValue() { var x=document.getElementById("myHeader") alert(x.innerHTML) } </script> </head> <body> <h1 id="myHeader" onclick="getValue()">This is a header</h1> <p>Click on the header to alert its value</p> </body> </html>
本文介绍JavaScript中使用getElementById()方法获取页面元素的基本语法及示例,并对比getElementsByName()和getElementsByTagName()等方法。
36万+

被折叠的 条评论
为什么被折叠?



