document.open() 与 document.write()的区别

时间:2021-05-28

document.open()打开一个新的空白文档,在IE下,open有两个默认参数,相当于document.open("text/html",'""),第二个参数只有一个值可选:replace,如果启用了该值,则新建的文档会覆盖当前页面的文档(相当于清空了原文档里的所有元素,且不能后退即,浏览器的后退按钮不可用);
看一个例子:

<SCRIPTLANGUAGE="JavaScript">
<!--
functiontest(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<inputtype='button'value='back(第二个按钮)'onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<inputtype='button'value='back(第三个按钮)'onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<inputtype='button'value='back(第四个按钮)'onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<inputtype="button"value="第一个按钮"onclick="test()">

平常都不写document.open()与document.close(),因为浏览器会在write之前先open一个文档,再把write的内容输出到原文档里面。write结束后,默认是不会有close的,否则第二行document.write的时候就会覆盖之前的write。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章