--- title: document.write slug: Web/API/Document/write tags: - API - DOM - Document - write - 参考 - 方法 translation_of: Web/API/Document/write --- <div>{{ApiRef("DOM")}}</div> <p><strong><code>Document.write()</code></strong> 方法将一个文本字符串写入一个由 {{domxref("document.open()")}} 打开的文档流(document stream)。</p> <div class="note"> <p><strong>注意</strong>: 因为 <code>document.write</code> 需要向文档<strong>流</strong>中写入内容,所以,若在一个已关闭(例如,已完成加载)的文档上调用 <code>document.write</code>,就会自动调用 <code>document.open</code>,<a href="/zh-CN/docs/Web/API/document.open#Notes">这将清空该文档的内容</a>。</p> </div> <h2 id="语法">语法</h2> <pre class="brush: js"><var>document</var>.write(<var>markup</var>);</pre> <h3 id="参数">参数</h3> <dl> <dt><var>markup</var></dt> <dd><em>一个包含要写入文档的文本的字符串。</em></dd> </dl> <h3 id="示例">示例</h3> <pre class="brush: html"><html> <head> <meta charset="UTF-8"> <title><code>document.write()</code> example</title> <code><script> function newContent() { document.open(); document.write("<h1>Out with the old - in with the new!</h1>"); document.close(); } </script></code> </head> <body <code>onload="newContent();"</code>> <p>Some original document content.</p> </body> </html> </pre> <p>{{EmbedLiveSample("Syntax")}}</p> <h2 id="备注">备注</h2> <p>向一个已经加载,并且没有调用过 {{domxref("document.open()")}} 的文档写入数据时,会自动调用 <code>document.open</code>。一旦完成了数据写入,建议调用 {{domxref("document.close()")}},以告诉浏览器当前页面已经加载完毕。写入的数据会被解析到文档结构模型(DOM)里。在上面的例子里,元素 <code>h1</code> 会成为文档中的一个节点。</p> <p>如果 <code>document.write()</code> 调用发生在 HTML 里的 <code><script></code> 标签中,那么它将不会自动调用 <code>document.open()</code>。详见如下例子:</p> <pre class="brush: html"><script> document.write("<h1>Main title</h1>") </script> </pre> <div class="note"><strong>注意:</strong><code>document.write</code> 和 {{domxref("document.writeln")}} <a href="https://developer.mozilla.org/en-US/docs/Archive/Web/Writing_JavaScript_for_HTML">在 XHTML 文档中不可用</a>(控制台上会显示 "Operation is not supported"[<code>NS_ERROR_DOM_NOT_SUPPORTED_ERR</code>] 的报错信息)。 当打开本地的 .xhtml 格式的文件或任何其他 {{Glossary("MIME type", "MIME 类型")}}为 <code>application/xhtml+xml</code> 的文档时,均会报错。更多信息可查看 <a href="http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite">W3C XHTML FAQ</a>。</div> <div class="note"><strong>注意:</strong>在有<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer">deferred</a> 或 <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async">asynchronous</a> 属性的 script 中,<code>document.write</code> 会被忽略,控制台会显示 "A call to <code>document.write()</code> from an asynchronously-loaded external script was ignored" 的报错信息。</div> <div class="note"><strong>注意:</strong>在 Edge 中,在 {{HTMLElement("iframe")}} 内部调用 <code>document.write</code> 多于一次时会引发错误 SCRIPT70: Permission denied。</div> <div class="note"><strong>注意:</strong>从 Chrome 55 开始,Chrome(可能)不会运行通过 <code>document.write()</code> 注入的<code><script></code>,以防止使用 2G 连接的用户找不到 HTTP 缓存。前往<a href="https://developers.google.cn/web/updates/2016/08/removing-document-write">此链接</a>查看这种情况发生需要满足的条件。</div> <h2 id="规范" name="规范">规范</h2> <table> <thead> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">备注</th> </tr> </thead> <tbody> <tr> <td>{{SpecName("HTML WHATWG", "#dom-document-write", "document.write(...)")}}</td> <td>{{Spec2("HTML WHATWG")}}</td> <td></td> </tr> <tr> <td>{{SpecName("DOM2 HTML", "html.html#ID-75233634", "document.write(...)")}}</td> <td>{{Spec2("DOM2 HTML")}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.Document.write")}}</p> <h2 id="相关链接" name="相关链接">参见</h2> <ul> <li>{{ domxref("element.innerHTML") }}</li> <li>{{ domxref("document.createElement()") }}</li> </ul>