<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="text/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技巧" >