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>

猜你喜欢

转载自www.cnblogs.com/Alex1994/p/9790966.html