代码改变世界

读写元素属性

2011-08-21 16:10  鹏雕  阅读(148)  评论(0编辑  收藏  举报

读写属性1:
    <p>
        <a id="antares" href="test1.htm" title="Marssion">Antares</a>
    </p>
    var anchor=document.getElementById("antares");
    var anchorId=anchor.getAttribute("id");
    var anchorTitle=anchor.getAttribute("title");
    alert("The anchor ID is:"+anchorId+"\nThe anchor title is:"+anchorTitle);
   
读写属性2:
       <p>
        <a id="antares" href="test2.htm" title="Marssion">Antares</a>
    </p>
      var anchor=document.getElementById("antares");
    anchor.setAttribute("title","Marssion.lee");
    var newTitle=anchor.getAttribute("title");
    alert("The anchor title is:"+newTitle);