<div class="codetitle"> <span><a style="CURSOR: pointer" data="69253" class="copybut" id="copybut69253" onclick="doCopy('code69253')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code69253"> <br> <br><script language="javascript" type="javascript"> <BR>function test(){ <BR>//建立元素<BR>var myElement = document.createElement("a");//a是想建立的html元素標籤名稱<BR> //為創建的元素添加必要的資訊<BR>myElement.href = "http://www.baidu.com"; <BR>myElement.innerText = "連接到百度"; <BR>myElement.id = " id1"; <BR>//myElement.style.top = "300px"; <BR>//myElement.style.left = "500px"; <BR>//myElement.style.position = "absolute"; <BR>//把建立的元素加到body 物件上<BR>//document.body.appendChild(myElement); <BR>//將元素加入div <BR>document.getElementById("div1").appendChild( myElement); <BR>} <BR>function test2(){ <BR>//刪除一個元素<BR>//也可以透過屬性取得新增元素的父元素<BR>//document.getElementById("id1 ").parentNode <BR>document.getElementById("div1").removeChild(document.getElementById("id1")); <BR>} <BR></script> <br> <br><input type="button" onclick="test()" value="動態的建立一個超連結"> <br><input type="button" onclick="test2()" value="動態的刪除新增的元素"> <br><div id="div1" style="width:200px;height:300px;border:1px solid red"> <br>div1 <br> </div> <br> <br> <br> </div> <br><img src="http://files.jb51.net/file_images/article/201401/201401241521082.gif?2014024152123" alt="動態的創建一個元素createElement及刪除一個元素_javascript技巧" >