<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> </head> <body> <div><b id="oldnode">JavaScript</b>是一个很常用的技术,为网页添加动态效果。</div> <a href="javascript:replaceMessage()"> 将加粗改为斜体加粗</a> <script type="text/javascript"> function replaceMessage(){ var newnode1=document.createElement("i"); var oldnode=document.getElementById("oldnode"); newnode1.innerHTML=oldnode.innerHTML; oldnode.parentNode.replaceChild(newnode1,oldnode); var newnode2=document.createElement("b"); newnode1.appendChild(newnode2); newnode2.innerHTML=newnode1.innerHTML; } </script> </body> </html>
我知道了,插入节点不对,哦了~
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> </head> <body> <div><b id="oldnode">JavaScript</b>是一个很常用的技术,为网页添加动态效果。</div> <a href="javascript:replaceMessage()"> 将加粗改为斜体加粗</a> <script type="text/javascript"> function replaceMessage(){ var newnode1=document.createElement("i"); var oldnode=document.getElementById("oldnode"); newnode1.innerHTML=oldnode.innerHTML;//第一个斜体的"JavaScript",这是多出来的那个,注释掉改行 oldnode.parentNode.replaceChild(newnode1,oldnode); var newnode2=document.createElement("b"); newnode1.appendChild(newnode2); newnode2.innerHTML=newnode1.innerHTML;//第二个斜体加粗的"JavaScript" } </script> </body> </html>