通过ID获取元素getElementById

语法:

 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>

 

posted @ 2015-08-11 16:38  crazyBoy-xp  阅读(205)  评论(0编辑  收藏  举报