使用getElementById访问修改元素

来源:互联网 发布:wmv破解软件 编辑:程序博客网 时间:2024/06/12 23:04


<!DOCTYPE HTML>

<html lang="en">
<head>
  <title>Planets</title>
  <meta charset"utf-8">
</head>
<body>
   <h1>First Planet</h1>
     <p id="firstplanet">all is well</p>
    <h1>Second Planet</h1>
     <p id="secondplanet">nothing to report</p>
    <h1>Third Planet</h1>
     <p id="thirdplanet">all systems A-ok</p> 
  </body>

</html>


 修改后

<!DOCTYPE HTML>
<html lang="en">
<head>
  <title>Planets</title>
  <meta charset"utf-8">
 
</head>
<body>
   <h1>First Planet</h1>
     <p id="firstplanet">all is well</p>
    <h1>Second Planet</h1>
     <p id="secondplanet">nothing to report</p>
    <h1>Third Planet</h1>
     <p id="thirdplanet">all systems A-ok</p> 
  </body>

 <script>
     function init(){
       var planet=document.getElementById("firstplanet");
       planet.innerHTML="Red Alert:hit by phaser fire";
     }
     window.onload=init;  
  </script>

</html>


<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>


0 0