DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javaScript(JS)替換節點實現思路介紹
javaScript(JS)替換節點實現思路介紹
編輯:關於JavaScript     
復制代碼 代碼如下:
<title></title>
<script type="text/javascript">
function createNode() {
var pNode = document.createElement('p');
var tNode = document.createTextNode('煙花三月下楊州');
pNode.appendChild(tNode);
document.body.appendChild(pNode);
}
function r() {
var pNode = document.createElement('p');
var tNode = document.createTextNode('故人西辭黃鶴樓');
pNode.appendChild(tNode);
//獲取要替換的節點
var reNode = document.getElementsByTagName('p')[0];
//這種方法只適用於IE浏覽器
//reNode.replaceNode(pNode, reNode);
//適用於各種浏覽器
reNode.parentNode.replaceChild(pNode, reNode);
}
function reNode() {
var oldNode = document.getElementsByTagName('p')[0];
//oldNode.parentNode返回的是p節點的父節點,這裡就是body
//然後使用body節點的removeChild方法刪除下的oldNode節點
oldNode.parentNode.removeChild(oldNode);
}
</script>
</head>
<body>
<input id="Button1" type="button" value="創建節點" onclick="createNode()"/>
<input id="Button2" type="button" value="替換節點" onclick="r()" />
<input id="Button3" type="button" value="刪除節點" onclick="reNode()" />
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved