当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 您只能在 HTML 输出流中使用 document.write,啥意思

您只能在 HTML 输出流中使用 document.write,啥意思

2018年10月24日  | 移动技术网IT编程  | 我要评论
咬文嚼字 :您只能在 HTML 输出流中使用 document.write ...

 javascript ;写入到html输出语法 注意:只能在html输出中使用,如果在文档已经加载后使用(比如在函数中)  会覆盖到整个文档 <!doctype html> <html> <body>

<p> javascript 能够直接写入 html 输出流中: </p>

<script> document.write("<h1>this is a heading</h1>");//p1 document.write("<p>this is a paragraph.</p>");//p2 </script>

<p> 您只能在 html 输出流中使用 <strong>document.write</strong>。 如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。 </p>

</body> </html>

刚学js看了这句话,一脸的懵逼 最后参考了网上的一些解析,加上实验很快明白了什么意思

解析前半句 您只能在html输出流中使用document.write

html输出流是什么? html 输出流指的是当前数据形式是html格式的数据,这部分数据正被导出、传输或显示,所以称为”流“。

比如上面的p1和p2就是输出流的形式;

<!doctype html> <html> <body>

<p> javascript 能够直接写入 html 输出流中: </p> <button onclick="myfunction()">点击这里<\button> <script> document.write("<h1>this is a heading</h1>");//p1 document.write("<p>this is a paragraph.</p>");//p2 function myfunction(){

 document.write("覆盖之前的和h1和p 及覆盖所有的文档");     } </script>

<p>

原因吧? 以下引用其他网友 document.write是文件创建过程中创建内容

文档在创建过程中,如果遇到document.write就把内容加入到document中

当文档已经创建完成,及document已经关闭,此时在遇到document.write就会重写document 如果文档已完成加载后再执行document.write就会把整个html覆盖

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网