zoukankan      html  css  js  c++  java
  • document.write 简介

    document.write 的执行分两种情况:

    第一种:dom加载已完成

    1. 执行 document.open() (即会清空document)

    2. 执行 document.write()

    3. 执行 document.close()

    看例子

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
      
    </head>
    <body>
      <p>
        This is a p tag
      </p>
      <script>
         function write() {
            document.write('nothing left')
         }
         setTimeout(write, 1000)
      </script>
    </body>
    </html>

    一秒后整个document会清除,包括script标签,最后只留下body中的 'nothing left'

    第二种:dom未加载完成

    1. document.write() (由于js是阻塞的,页面已经在document已经在open状态,所以不会清空document)

    2. document.close()

    看例子

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
      
    </head>
    <body>
      <p>
        This is a p tag
      </p>
      <script>
        function write() {
            document.write('nothing left')
        }
        write()
      </script>
    </body>
    </html>

    执行后的结果是除了那个p标签,页面的body最后又加了一个 'nothing left'

    参考链接:http://devdocs.io/dom/document/write

  • 相关阅读:
    bzoj3272 Zgg吃东西
    bzoj3894 文理分科
    poj1149 PIGS
    poj1637 Sightseeing tour
    [Wc2007]剪刀石头布
    poj2396 Budget
    [NOI2017]游戏
    CF666E Forensic Examination
    bzoj4889 [Tjoi2017]不勤劳的图书管理员
    CF587F Duff is Mad
  • 原文地址:https://www.cnblogs.com/savokiss/p/6670799.html
Copyright © 2011-2022 走看看