首页 > 其他 > 详细

通过ID获取元素getElementById

时间:2015-08-11 17:38:10      阅读:221      评论:0      收藏:0      [点我收藏+]

语法:

 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>

 

通过ID获取元素getElementById

原文:http://www.cnblogs.com/crazy-IT-Boy/p/4721437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!