5. Document open() 方法

定义和用法

open() 方法可打开一个新文档,并擦除当前文档的内容。
语法

document.open(mimetype,replace)

这里写图片描述

说明

该方法将擦除当前 HTML 文档的内容,开始一个新的文档,新文档用 write() 方法或 writeln() 方法编写。

提示和注释

重要事项:调用 open() 方法打开一个新文档并且用 write() 方法设置文档内容后,必须记住用 close 方法关闭文档,并迫使其内容显示出来。

注释:属于被覆盖的文档的一部分的脚本或事件句柄不能调用该方法,因为脚本或事件句柄自身也会被覆盖。


实例

<html>
<head>
<script type="text/javascript">
function createNewDoc()
  {
  var newDoc=document.open("text/html","replace");
  var txt="<html><body>Learning about the DOM is FUN!</body></html>";
  newDoc.write(txt);
  newDoc.close();
  }
</script>
</head>
<body>

<input type="button" value="Write to a new document"
onclick="createNewDoc()">

</body>
</html>

在新窗口打开新的文档,并添加一些文本。

<html>
<head>
<script type="text/javascript">
function winTest()
  {
  var txt1 = "This is a new window.";
  var txt2 = "This is a test.";
  win.document.open("text/html","replace");
  win.document.writeln(txt1);
  win.document.write(txt2);
  win.document.close();
  }
</script>
</head>
<body>

<script type="text/javascript">
var win=window.open('','','width=200,height=200');
winTest();
</script>

</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值