document.open() 與 document.write()的區(qū)別
更新時間:2007年08月13日 19:53:13 作者:
document.open() 打開一個新的空白文檔,在IE下,open有兩個默認參數(shù),相當于document.open("text/html",'""),第二個參數(shù)只有一個值可選:replace,如果啟用了該值,則新建的文檔會覆蓋當前頁面的文檔(相當于清空了原文檔里的所有元素,且不能后退即,瀏覽器的后退按鈕不可用);
看一個例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個按鈕" onclick="test()">
平常都不寫document.open() 與 document.close(),因為瀏覽器會在write之前先open一個文檔,再把write的內(nèi)容輸出到原文檔里面。write結束后,默認是不會有close的,否則第二行document.write的時候就會覆蓋之前的write。
看一個例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三個按鈕)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四個按鈕)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一個按鈕" onclick="test()">
平常都不寫document.open() 與 document.close(),因為瀏覽器會在write之前先open一個文檔,再把write的內(nèi)容輸出到原文檔里面。write結束后,默認是不會有close的,否則第二行document.write的時候就會覆蓋之前的write。
相關文章
用循環(huán)或if語句從json中取數(shù)據(jù)示例
倘若想將id和pid數(shù)據(jù)依次取出,就只能用循環(huán),若想有選擇性的輸出時,需要添加if條件2014-08-08
javascript利用canvas實現(xiàn)鼠標拖拽功能
這篇文章主要為大家詳細介紹了javascript利用canvas實現(xiàn)鼠標拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07

