document.write()会清空原来的内容原因

http://www.softwhy.com/forum.php?mod=viewthread&tid=13582

<meta name="author" content="http://www.softwhy.com/" />      
<title>蚂蚁部落</title>    
<script type="text/javascript">  
window.onload=function(){
  document.write("分享互助");
}
</script>
</head>  
<body>  
<div>蚂蚁部落欢迎您</div>
</body>  
</html>

从以上代码的可以看出document.write()函数将原来的文档内容清空了,下面介绍一下出现此种情况的原因: <meta name="author" content="http://www.softwhy.com/" />      
<title>蚂蚁部落</title>    
<script type="text/javascript">  
document.write("分享互助");
</script>
</head>  
<body>  
<div>蚂蚁部落欢迎您</div>
</body>  
</html>

在以上代码中,原来的文档内容并没有被清空,这是因为当前文档流是由浏览器所创建,

并且document.wirte()函数身处其中,也就是执行此函数的时候文档流并没有被关闭,

这个时候不会调用document.open()函数创建新文档流,所以也就不会被覆盖了。

可能还有朋友会问为什么下面的方式还是不行,

代码如下:
<!DOCTYPE html>      
<html>      
<head>      
<meta charset=" utf-8">      
<meta name="author" content="http://www.softwhy.com/" />      
<title>蚂蚁部落</title>    
<script type="text/javascript">
document.close();
document.write("分享互助");
</script>
</head>  
<body>  
<div>蚂蚁部落欢迎您</div>
</body>  
</html>

上面使用document.close()关闭文档流了,为什么还是不能够覆盖原来的内容的,很遗憾,文档流是由浏览器创建,

无权限手动关闭,document.close()函数只能够关闭由document.open()函数创建的文档流。

看下面的代码实例:
<!DOCTYPE html>      
<html>      
<head>      
<meta charset=" utf-8">      
<meta name="author" content="http://www.softwhy.com/" />      
<title>蚂蚁部落</title>      
<script type="text/javascript">  
function create(){  
  var newWindow=window.open("","蚂蚁部落","_blank");  
  newWindow.document.write("蚂蚁部落欢迎您");  
  newWindow.document.close();  
  newWindow.document.write("ABC");
}  
window.onload=function(){  
  var obt=document.getElementById("bt");  
  obt.onclick=function(){  
    create();  
  }  
}  
</script>  
</head>  
<body>  
<div id="print">蚂蚁部落欢迎您,只有努力奋斗才会有美好的明天。</div>  
<input type="button" id="bt" value="查看效果"/>  
</body>  
</html>

由doucment.open()创建的文档流就可以由document.close()关闭,那么第二个document.write()输出的内容会覆盖掉第一个输出的内容。

时间: 2024-08-10 17:17:32

document.write()会清空原来的内容原因的相关文章

为什么document.write()会清空原来的内容

为什么document.write()会清空原来的内容: 可能很多朋友都遇到过这样的情况,那就是使用document.write()函数向网页中写内容的时候,会把文档中的原来的内容给清空,这一点对于初学者来说算是一个困扰,下面就介绍一下为什么会出现这种情况,当然也就知道如何避免此种情况的发生了.先看一段代码实例: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta

remove() 删除节点 | detach() 删除节点 | empty() 清空节点的内容 | clone() 复制节点

<html> <head> <title></title> <script src="jquery-2.1.3.js"></script> </head> <body> <div> <ul> <li>中国</li> <li>美国</li> <li>德国</li> <li>俄国</

清空文件的内容 和 统计文件的大小的命令

清空文件的内容:true > 文件名  (或 echo “”> 文件名) 统计文件的大小:du -h --max-depth=1 /usr/local/nginx/logs/* (*表示logs文件夹里的所有文件) 统计文件夹的大小:du -sh 文件夹

VB.NET-轻松判断文本框是否为空,一键清空文本框内容

房收费系统的时候,令人相当恶心的一件事就是判断文本框和组合框是否为空还有清空文本框.基本上每个窗体都要进行判断,那一个接着一个的If...Else...语句,长长一串,看着就头疼,但是第一次做机房收费系统的时候竟然傻傻的一个不落都写了出来.真佩服当时的自己,不过这一次还是不要在做那种傻事了,因为我们经历了不少面向对象的洗礼,认识了封装. 向对象的范畴里,对于相同的或者类似的代码只要重复3次以上我们应该想到面向对象的三大特性之一:封装.想是想到了但是具体怎么通过封装来简化繁琐的判断任务呢? 1.

清空iframe的内容

document.frames["home"].document.write(""); 我都是用这种方法往iframe里面添加内容的.document.frames["iframe1"].document.write("<img src='loading.gif'>"); 请问怎么才能清空iframe1里面的内容?不要用以下两种方法:document.frames["iframe1"].docu

JavaScript-2.2 document.write 输出到页面的内容

<html> <head> <meta http-equiv="content-type" content="text/html;charset=GB2312"/> <title> 2-4 使用document.write经内容输出到页面 </title> <!--脚本部分--> <script type="text/javascript"> document.w

关于docnment.write() 会清空原来的内容

情况一: <script type="text/javascript">  window.onload=function(){   document.write("分享互助"); } </script> </head>  <body>  <div>蚂蚁部落欢迎您</div> </body>   document.write()函数将原来的文档内容清空了 因为: window.onlo

[JQuery] 真正意义上清空表单内容

JQuery :not() 选择器 定义与用法: :not() 选择器选取除了指定元素以外的所有元素. 最常见的用法:与其他选择器一起使用,选取指定组合中除了指定元素以外的所有元素. 参考代码: <script> $(function() { $("p:not(#p1)").css("color", "red"); //写法一 $("p").not("#p1").css("color&

java io流清空txt文件内容及填充文件内容

File file = new File(this.getClass().getResource("").getPath());System.out.println(file.getPath());String path=file.getPath();String filename="test1.txt";//path=path.substring(0,path.indexOf("WEB-INF"))+"upload\\Excel&qu