document.open() 與 document.write()的區(qū)別
更新時(shí)間:2007年08月13日 19:53:13 作者:
document.open() 打開一個(gè)新的空白文檔,在IE下,open有兩個(gè)默認(rèn)參數(shù),相當(dāng)于document.open("text/html",'""),第二個(gè)參數(shù)只有一個(gè)值可選:replace,如果啟用了該值,則新建的文檔會(huì)覆蓋當(dāng)前頁面的文檔(相當(dāng)于清空了原文檔里的所有元素,且不能后退即,瀏覽器的后退按鈕不可用);
看一個(gè)例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個(gè)按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個(gè)按鈕" onclick="test()">
平常都不寫document.open() 與 document.close(),因?yàn)闉g覽器會(huì)在write之前先open一個(gè)文檔,再把write的內(nèi)容輸出到原文檔里面。write結(jié)束后,默認(rèn)是不會(huì)有close的,否則第二行document.write的時(shí)候就會(huì)覆蓋之前的write。
看一個(gè)例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個(gè)按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個(gè)按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個(gè)按鈕" onclick="test()">
平常都不寫document.open() 與 document.close(),因?yàn)闉g覽器會(huì)在write之前先open一個(gè)文檔,再把write的內(nèi)容輸出到原文檔里面。write結(jié)束后,默認(rèn)是不會(huì)有close的,否則第二行document.write的時(shí)候就會(huì)覆蓋之前的write。
您可能感興趣的文章:
- js中document.write使用過程中的一點(diǎn)疑問解答
- js document.write()使用介紹
- document.write()及其輸出內(nèi)容的樣式、位置控制
- document.write與writeln的輸出內(nèi)容區(qū)別說明
- 在網(wǎng)頁中使用document.write時(shí)遭遇的奇怪問題
- 第一個(gè)JavaScript入門基礎(chǔ) document.write輸出
- 代碼生成器 document.write()
- document.open() 與 document.write()
- JS 中document.write()的用法和清空的原因淺析
相關(guān)文章
用循環(huán)或if語句從json中取數(shù)據(jù)示例
倘若想將id和pid數(shù)據(jù)依次取出,就只能用循環(huán),若想有選擇性的輸出時(shí),需要添加if條件2014-08-08
javascript利用canvas實(shí)現(xiàn)鼠標(biāo)拖拽功能
這篇文章主要為大家詳細(xì)介紹了javascript利用canvas實(shí)現(xiàn)鼠標(biāo)拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

