推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
hm20062006ok

document 的 DOMContentLoaded 事件什么时候 readyState === 'complete' ?

  •  
  •   hm20062006ok · May 17, 2021 · 2320 views
    This topic created in 1847 days ago, the information mentioned may be changed or developed.

    在文档最底部插入以下脚本:

    document.addEventListener("DOMContentLoaded", function(event) { console.log(event.target.readyState); //这里的 readyState 一直是 'interactive' 吗? }, false);

    6 replies    2021-05-20 13:42:07 +08:00
    AV1
        1
    AV1  
       May 17, 2021   ❤️ 1
    hm20062006ok
        2
    hm20062006ok  
    OP
       May 17, 2021
    @DOLLOR 好的。 但我还是好奇它在什么情况下会是 complete
    xiaopc
        3
    xiaopc  
       May 17, 2021 via Android
    HTML 被全部加载并且解析为 DOM 后就触发 DOMContentLoaded ; readyState 有三种状态,在子资源全部加载完成后才是 complete 。

    MDN 一查就有解释
    hm20062006ok
        4
    hm20062006ok  
    OP
       May 17, 2021
    @xiaopc 为了“子资源全部加载完成”, 我放了一张大图片。如下代码,可是还是没有监听到 complete?

    https://gist.github.com/hm20062006ok/80fad49a847d83461eeaa2656ddae29e
    xiaopc
        5
    xiaopc  
       May 18, 2021 via Android
    ......
    你这段代码监听的是 DOMContentLoaded,那时候当然没加载完成啊。要按 #1 说的监听 readystatechange 事件。
    jokerai
        6
    jokerai  
       May 20, 2021
    DOM 解析生命周期
    异步脚本、延迟脚本与 DOMContentLoaded 的关系
    https://www.cnblogs.com/Bonnie3449/p/8419609.html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3279 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 11:53 · PVG 19:53 · LAX 04:53 · JFK 07:53
    ♥ Do have faith in what you're doing.