替換元素節(jié)點(diǎn)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</title>
</head>
<body>
? <div><b id="oldnode">JavaScript</b>是一個很常用的技術(shù),為網(wǎng)頁添加動態(tài)效果。</div>
? <a href="javascript:replaceMessage()"> 將加粗改為斜體</a>
?
??? <script type="text/javascript">
????? function replaceMessage(){
??????? var newnode=document.createElement("i");
??????? var newnodeText=document.createTextNode("javaScript");
??????? newnode.appendChild(newnodeText);
??????? var oldNode=document.getElementById("oldnode");
??????? oldNode.parentNode.replaceChild(newnode,oldNode);
?? ??? ? ?
?????? }?? ?
? </script>
?
?</body>
</html>
上面的appendChild不是在節(jié)點(diǎn)的后面插入一個節(jié)點(diǎn)嗎。前面一個文本節(jié)點(diǎn),后面一個元素為i的元素節(jié)點(diǎn),為什么顯示的結(jié)果是正確的呢
2016-04-11
appendChild是在 i 標(biāo)簽里插入的文本節(jié)點(diǎn),?oldNode.parentNode.replaceChild(newnode,oldNode);這替換的是元素節(jié)點(diǎn)
2016-04-11
不用謝