HTML DOM open() 方法
htmlweb developmentfront end technology更新于 2025/6/15 15:40:17
DOM open() 方法使用其在 HTML 文档中的名称,将其参数中指定的节点设置为属性节点。
语法
以下是语法 −
document.open(type,replace);
其中 type 表示文档类型,replace 表示新文档的历史记录条目是否继承打开此文档的文档的历史记录条目。
示例
让我们看一个 open() 方法的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align:center;
color:#fff;
background: #ff7f5094;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
</style>
</head>
<body>
<h1>DOM open() Method Demo</h1>
<button onclick="openMethod()" class="btn">Click me to try Open() Mehtod</button>
<script>
function openMethod() {
document.open();
document.write(`
<style>
body{
text-align:center;
color:#fff;
background: #ff7f5094;
}
</style>
<h1>DOM open() Method Demo</h1>
<p>New document</p>
`);
document.close();
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击“蓝色”按钮使用 open() 方法打开一个新的输出流。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

