JavaScript中document.open()方法的使用指南

作者:竹影

时间:2026-10-27

浏览:431

本文介绍JavaScript中document.open()方法的使用。该方法用于打开新文档,配合document.write()写入内容,最后通过document.close()关闭并显示。文章提供了完整的代码示例,展示如何在页面加载时动态生成文本内容。

文章目录

document.open()方法概述

document.open()方法用于打开一个新的文档流,随后可以使用document.write()方法向该文档写入内容,最后调用document.close()方法关闭文档操作,从而使写入的内容在页面上显示出来。



具体使用示例如下:


代码实现如下:

 
代码示例<script type="text/javascript">
window.onload=hello;
function hello(){
var msg = "JavaScript真好玩!";
document.open();
document.write(msg);
document.close();
} 
</script>

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章