aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/document/domcontentloaded_event/index.html
blob: 62b7c07d1ae82eb780aed3233c2fb252daaba415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
title: 'Document: DOMContentLoaded 事件'
slug: Web/API/Document/DOMContentLoaded_event
tags:
  - 事件
translation_of: Web/API/Document/DOMContentLoaded_event
---
<div>{{APIRef}}</div>

<p>当纯HTML被完全加载以及解析时,<strong><code>DOMContentLoaded</code> </strong>事件会被触发,而不必等待样式表,图片或者子框架完成加载。</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">冒泡阶段</th>
   <td>Yes</td>
  </tr>
  <tr>
   <th scope="row">可撤销性</th>
   <td>Yes (尽管它被指定为一个简单事件时是不可撤销的)</td>
  </tr>
  <tr>
   <th scope="row">接口</th>
   <td>{{domxref("Event")}}</td>
  </tr>
  <tr>
   <th scope="row">事件句柄属性</th>
   <td>None</td>
  </tr>
 </tbody>
</table>

<p>一个易混用但不同的事件是,<code><a href="/en-US/docs/Web/API/Window/load_event">load</a></code>,这个事件仅仅应该在探测到整个页面完全加载完成时被使用。一个常见的错误就是在该使用<code>DOMContentLoaded</code>的地方使用了<code>load</code></p>

<p>JavaScript的同步模式会导致DOM解析暂停。如果你想在用户请求页面时,首先尽可能先解析DOM,此时你可以使用<a href="/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests">JavaScript异步模式</a>,并且<a href="https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery">优化样式表的加载</a>。在通常模式的加载过程中,样式表的加载会与DOM解析并行,从而迟缓主要HTMl文档的加载。</p>

<h2 id="例子"><strong>例子</strong></h2>

<h3 id="基本用法">基本用法</h3>

<pre class="brush: js">document.addEventListener('DOMContentLoaded', (event) =&gt; {
    console.log('DOM fully loaded and parsed'); // 译者注:"DOM完全加载以及解析"
});
</pre>

<h3 id="延迟_DOMContentLoaded"> 延迟 DOMContentLoaded</h3>

<pre class="brush: html">&lt;script&gt;
  document.addEventListener('DOMContentLoaded', (event) =&gt; {
    console.log('DOM fully loaded and parsed');
  });

for( let i = 0; i &lt; 1000000000; i++)
{} // 这段同步脚本将会延迟DOM解析,
   // 所以DOMContentLoaded事件将会延迟执行.
&lt;/script&gt;
</pre>

<h3 id="检查加载是否已经完成">检查加载是否已经完成</h3>

<p>在你的脚本有机会运行前,<code>DOMContentLoaded</code>可能就已经被触发。所以你在决定添加一个事件监听器前最好先检查一下。</p>

<pre class="brush: js">function doSomething() {
  console.info('DOM loaded');
}

if (document.readyState === 'loading') {  // 此时加载尚未完成
  document.addEventListener('DOMContentLoaded', doSomething);
} else {  // 此时`DOMContentLoaded` 已经被触发
  doSomething();
}
</pre>

<h3 id="实例">实例</h3>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">&lt;div class="controls"&gt;
  &lt;button id="reload" type="button"&gt;Reload&lt;/button&gt;
&lt;/div&gt;

&lt;div class="event-log"&gt;
  &lt;label&gt;Event log:&lt;/label&gt;
  &lt;textarea readonly class="event-log-contents" rows="8" cols="30"&gt;&lt;/textarea&gt;
&lt;/div&gt;</pre>

<div class="hidden">
<h4 id="CSS">CSS</h4>

<pre class="brush: css">body {
  display: grid;
  grid-template-areas: "control  log";
}

.controls {
  grid-area: control;
  display: flex;
  align-items: center;
  justify-content: center;
}

.event-log {
  grid-area: log;
}

.event-log-contents {
  resize: none;
}

label, button {
  display: block;
}

#reload {
  height: 2rem;
}

</pre>
</div>

<h4 id="JS">JS</h4>

<pre class="brush: js">const log = document.querySelector('.event-log-contents');
const reload = document.querySelector('#reload');

reload.addEventListener('click', () =&gt; {
  log.textContent ='';
  window.setTimeout(() =&gt; {
      window.location.reload(true);
  }, 200);
});

window.addEventListener('load', (event) =&gt; {
    log.textContent = log.textContent + 'load\n';
});

document.addEventListener('readystatechange', (event) =&gt; {
    log.textContent = log.textContent + `readystate: ${document.readyState}\n`;
});

document.addEventListener('DOMContentLoaded', (event) =&gt; {
    log.textContent = log.textContent + `DOMContentLoaded\n`;
});

</pre>

<h4 id="结果展示">结果展示</h4>

<p>{{ EmbedLiveSample('实例', '100%', '160px') }}</p>

<h2 id="规范"><strong>规范</strong></h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'parsing.html#the-end:event-domcontentloaded', 'DOMContentLoaded')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', 'parsing.html#the-end:event-domcontentloaded', 'DOMContentLoaded')}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.Document.DOMContentLoaded_event")}}</p>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>有关事件: <code><a href="/en-US/docs/Web/API/Window/load_event">load</a></code>, <code><a href="/en-US/docs/Web/API/Document/readystatechange_event">readystatechange</a></code>, <code><a href="/en-US/docs/Web/API/Window/beforeunload_event">beforeunload</a></code>, <code><a href="/en-US/docs/Web/API/Window/unload_event">unload</a></code></li>
 <li><code><a href="/en-US/docs/Web/API/Window">Window</a></code> 对象中的此事件: <code><a href="/en-US/docs/Web/API/Window/DOMContentLoaded_event">DOMContentLoaded</a></code></li>
</ul>