精華區beta Ajax 關於我們 聯絡資訊
<div id="div1"> 今天天氣真好! </div> <script> var o=document.getElementById('div1'); var ot=document.createElement('b'); ot.appendChild(document.createTextNode('天氣')); o.replaceChild(ot,o.childNodes[0]); o.insertBefore(document.createTextNode('今天'),ot); o.appendChild(document.createTextNode('真好!')); </script> 這樣用不會比較好 但是幾乎把每個方法都玩到了@@ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.172.103.119
s25g5d4:順便說明一下 用innerHTML弄出來的都會被解譯為HTML格式 07/06 20:40
s25g5d4:用creatTextNode則會被視為純文字 所以上面的ot.appendChi 07/06 20:40
s25g5d4:ld(document.createTextNode('天氣'));改成<i>天氣</i>時 07/06 20:41
s25g5d4:就會變 今天<i>天氣</i>真好! 07/06 20:42