setNameditem()方法到底该怎么用呢,请师哥师姐们教教我!例子如下:
<html>
<head>
<script>
function get()
{var obody=document.body;
 var op=obody.childNodes[0];
 op.attributes.setNameItem("align");
 var t1=op.attributes.getNameItem("align");
t1.nodeValue="center"
 }
</script>
</head><body>
<p class="m1" id="d1">我们都是好人</p><form id="form1" name="form1" method="post" action="">
  <input type="button" name="button" id="button" value="按钮" onclick="get()"/>
</form>
</body>
</html>我想要的效果是,点击一下按钮 ,字会去中间。
但是总报错,不知道哪里写错了!!

解决方案 »

  1.   


    <html> 
    <head> 
    <script> 
    function get() 
    {var obody=document.body; 
      var op=obody.childNodes[0];
      var att = document.createAttribute("align");
      att.value = "center";
      op.attributes.setNamedItem(att); 
      //var t1=op.attributes.getNamedItem("align"); 
      //t1.nodeValue="right"; 

    </script> 
    </head> <body> 
    <p class="m1" id="d1">我们都是好人 </p> <form id="form1" name="form1" method="post" action=""> 
      <input type="button" name="button" id="button" value="按钮" onclick="get()"/> 
    </form> 
    </body> 
    </html>