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


    情况一:
    <script type="text/javascript"
    window.onload=function(){
      document.write("分享互助");
    }
    </script>
    </head
    <body
    <div>蚂蚁部落欢迎您</div>
    </body>  
    document.write()函数将原来的文档内容清空了
      因为:
        window.onload事件是在文档内容完全加载完毕再去执行事件处理函数,当然文档流已经关闭了,这个时候执行doucment.writ()函数会自动调用document.open()函数创建一个新的文档流,并写入新的内容,再通过浏览器展现,这样就会覆盖原来的内容。
     
     
    情况二:
      
    <script type="text/javascript"
    document.write("分享互助");
    </script>
    </head
    <body
    <div>蚂蚁部落欢迎您</div>
    </body
    原来的文档内容并没有被清空
      因为:
        当前文档流是由浏览器所创建,并且document.wirte()函数身处其中,也就是执行此函数的时候文档流并没有被关闭,这个时候不会调用document.open()函数创建新文档流,所以也就不会被覆盖了。
     
     
    情况三:
    <script type="text/javascript">
    document.close();
    document.write("分享互助");
    </script>
    </head
    <body
    <div>蚂蚁部落欢迎您</div>
    </body
    原来的文档内容没有被清空
      因为:
        文档流是由浏览器创建,无权限手动关闭,document.close()函数只能够关闭由document.open()函数创建的文档流
     
    情况四:
      
    <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
    有doucment.open()创建的文档流就可以由document.close()函数关闭,那么第二个document.write()输出的内容会覆盖掉第一个输出的内容。
     
     
  • 相关阅读:
    257. Binary Tree Paths
    324. Wiggle Sort II
    315. Count of Smaller Numbers After Self
    350. Intersection of Two Arrays II
    295. Find Median from Data Stream
    289. Game of Life
    287. Find the Duplicate Number
    279. Perfect Squares
    384. Shuffle an Array
    E
  • 原文地址:https://www.cnblogs.com/sao-di-seng/p/5520848.html
Copyright © 2020-2023  润新知