<div class="codetitle"> <span><a style="CURSOR: pointer" data="65923" class="copybut" id="copybut65923" onclick="doCopy('code65923')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code65923"> <br><title></title> <br><br> <br><title></title> <br><script type=" text/javascript"> <BR>function createNode() { <BR>var pNode = document.createElement('p'); <BR>var tNode = document.createTextNode('煙火三月下楊州'); <BR>pNode.appendChild(tNode); <BR>document.body.appendChild(pNode); <BR>} <BR>function r() { <BR>var pNode = document.createElement('p'); <BR> var tNode = document.createTextNode('故人西辭黃鶴樓'); <BR>pNode.appendChild(tNode); <BR>//取得要替換的節點<BR>var reNode = document.getElementsByTagName('p' )[0]; <BR>//這個方法只適用於IE瀏覽器<BR>//reNode.replaceNode(pNode, reNode); <BR>//適用於各種瀏覽器<BR>reNode.parentNode .replaceChild(pNode, reNode); <BR>} <BR>function reNode() { <BR>var oldNode = document.getElementsByTagName('p')[0]; <BR>//oldNode.parentNode回傳的是p節點的父節點,這裡是body <BR>//然後使用body節點的removeChild方法刪除下的oldNode節點<BR>oldNode.parentNode.removeChild(oldNode); <BR>} <BR></script> <br> <br> <br><input id="Button1" type="button" value="建立節點" onclick="createNode()"> <br><input id="Button2 " type="button" value="替換節點" onclick="r()"> <br><input id="Button3" type="button" value="刪除節點" onclick="reNode()"> </div>