请问下面这段代码中newDoc.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 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>
document.close();
document.write("xxxxsdsds");
但是加了close()那个方法也没有什么作用呀
<input type='button' value="Open and Close" onclick="Cls2();" />
<script>
function Cls1()
{
document.open();
document.write("<p>The one and only content.</p>");
}
function Cls2()
{
document.open();
document.write("<p>The one and only content.</p>");
document.close();
}
</script>如果没有close,你可以看到浏览器tab标签上的图标一直在转,说明文档内容一直没有输出完毕
document.close()
close() 方法可关闭一个由 document.open 方法打开的输出流,并显示选定的数据。
该方法将关闭 open() 方法打开的文档流,并强制地显示出所有缓存的输出内容。如果您使用 write() 方法动态地输出一个文档,必须记住当你这么做的时候要调用 close() 方法,以确保所有文档内容都能显示。一旦调用了 close(),就不应该再次调用 write(),因为这会隐式地调用 open() 来擦除当前文档并开始一个新的文档。
close()是关闭本次的文档内容,下面如用write() 会隐式地调用 open() 来擦除当前文档并开始一个新的文档。 <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(); //这时有无close()效果会明显不同
newDoc.write("测试效果的区别");
}
</script>
</head>
<body>
<input type="button" value="Write to a new document" onclick="createNewDoc()">
</body>
</html>