1. 程式人生 > >Javascript中只能在 HTML 輸出流中使用 document.write,在文件已載入後使用它(比如在函式中),會覆蓋整個文件。

Javascript中只能在 HTML 輸出流中使用 document.write,在文件已載入後使用它(比如在函式中),會覆蓋整個文件。

意思就是說,初次載入時如果沒有載入document.write,那麼再次載入的時候回覆蓋掉原來的內容,只顯示新載入的內容。

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="UTF-8">
 5   </head>
 6 <body>
 7 
 8  <p>
 9    JavaScript 能夠直接寫入 HTML 輸出流中:
10  </p>
11  <script>
12   document.write(
"<h1>This is a heading</h1>"); 13 document.write("<p>This is a paragraph.</p>"); 14 </script> 15 16 <p> 17 您只能在 HTML 輸出流中使用 <strong>document.write</strong>18 如果您在文件已載入後使用它(比如在函式中),會覆蓋整個文件。 19 </p> 20 21 <button onclick="myFunction()">
點選這裡</button> 22 23 <script> 24 function myFunction() 25 { 26 document.write("呼叫了函式,文件被重寫"); 27 } 28 </script> 29 30 </body> 31 </html>