document.write()怎么在JavaScript中使用-創(chuàng)新互聯(lián)

document.write()怎么在JavaScript中使用?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

十年的莊浪網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整莊浪建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“莊浪網(wǎng)站設(shè)計(jì)”,“莊浪網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
<!DOCTYPE html>   
<html>   
  <head>   
  <meta charset=" utf-8">      
  <title>Document</title>   
  <script type="text/javascript"> 
    window.onload=function(){
      document.write("重溫 JavaScript");
    }
  </script> 
</head> 
<body> 
  <div>Hello JavaScript</div> 
</body> 
</html>

  從以上代碼的可以看出document.write()函數(shù)將原來的文檔內(nèi)容清空了,下面介紹一下出現(xiàn)此種情況的原因:

  window.onload事件是在文檔內(nèi)容完全加載完畢再去執(zhí)行事件處理函數(shù),當(dāng)然文檔流已經(jīng)關(guān)閉了,這個(gè)時(shí)候執(zhí)行doucment.writ()函數(shù)會(huì)自動(dòng)調(diào)用document.open()函數(shù)創(chuàng)建一個(gè)新的文檔流,并寫入新的內(nèi)容,再通過瀏覽器展現(xiàn),這樣就會(huì)覆蓋原來的內(nèi)容。不過很多朋友還有會(huì)這樣的疑問,為什么類似下面的情況,原來網(wǎng)頁中的內(nèi)容不會(huì)被覆蓋,代碼如下:

<!DOCTYPE html>   
<html>   
  <head>   
  <meta charset=" utf-8">     
  <title>Document</title>   
  <script type="text/javascript"> 
    document.write("重溫 JavaScript");
  </script> 
</head> 
<body> 
  <div>Hello JavaScript</div> 
</body> 
</html>

  在以上代碼中,原來的文檔內(nèi)容并沒有被清空,這是因?yàn)楫?dāng)前文檔流是由瀏覽器所創(chuàng)建,并且document.wirte()函數(shù)身處其中,也就是執(zhí)行此函數(shù)的時(shí)候文檔流并沒有被關(guān)閉,這個(gè)時(shí)候不會(huì)調(diào)用document.open()函數(shù)創(chuàng)建新文檔流,所以也就不會(huì)被覆蓋了。可能還有朋友會(huì)問為什么下面的方式還是不行,代碼如下:

<!DOCTYPE html>   
<html>   
<head>   
  <meta charset=" utf-8">     
  <title>Document</title>   
  <script type="text/javascript">
    document.close(); 
    document.write("重溫 JavaScript");
  </script> 
</head> 
<body> 
  <div>Hello JavaScript</div> 
</body> 
</html>

  上面使用document.close()關(guān)閉文檔流了,為什么還是不能夠覆蓋原來的內(nèi)容的,很遺憾,文檔流是由瀏覽器創(chuàng)建,無權(quán)限手動(dòng)關(guān)閉,document.close()函數(shù)只能夠關(guān)閉由document.open()函數(shù)創(chuàng)建的文檔流??聪旅娴拇a實(shí)例:

<!DOCTYPE html>    
<html>    
<head>    
  <meta charset=" utf-8">      
  <title>Document</title>   
  <script type="text/javascript">  
  function create(){ 
    var newWindow=window.open("","Document","_blank"); 
    newWindow.document.write("Hello JavaScript"); 
    newWindow.document.close(); 
    newWindow.document.write("覆蓋后的輸出"); 
  } 
  window.onload=function(){ 
    var obt=document.getElementById("bt"); 
    obt.onclick=function(){ 
      create(); 
    } 
  } 
</script> 
</head>  
<body>  
  <div id="print">Hello JavaScript</div> 
  <input type="button" id="bt" value="查看效果"/> 
</body>  
</html>

  由doucment.open()創(chuàng)建的文檔流就可以由document.close()關(guān)閉,那么第二個(gè)document.write()輸出的內(nèi)容會(huì)覆蓋掉第一個(gè)輸出的內(nèi)容。

  異步引用外部JavaScript時(shí),必須先運(yùn)行document.open()清空文檔,然后才能運(yùn)行document.write(),參數(shù)寫在body內(nèi)容的開頭。

  如果不先運(yùn)行document.open(),直接運(yùn)行document.write(),則無效且Chrome有如下提示:

document.write()怎么在JavaScript中使用

// asyncWrite.js
document.open();
document.write('<p>test</p>');
document.close();
<!-- asyncWrite.html -->
<!-- 運(yùn)行前 -->
<body>
  <script src="asyncWrite.js" async></script>
</body>
<!-- 運(yùn)行后 -->
<body>
  <p>test</p>
</body>

  document.write()也能寫入含有script標(biāo)簽的字符串,但是需要轉(zhuǎn)義。寫入的script標(biāo)簽中的內(nèi)容會(huì)正常運(yùn)行。

<!-- 運(yùn)行前 -->
<script>
  document.write('<script>document.write("<p>test</p>");<\/script>');
</script>
<!-- 運(yùn)行后 -->
<script>
  document.write('<script>document.write("<p>test</p>");<\/script>');
</script>
<script>document.write("<p>test</p>");</script>
<p>test</p>

document.write()可以傳入多個(gè)參數(shù)。

<!-- 運(yùn)行前 -->
<body>
  <script>
    document.write('<h3>multiArgument</h3>','<p>test</p>');
  </script>
</body>
<!-- 運(yùn)行后 -->
<body>
  <script>
    document.write('<h3>multiArgument</h3>','<p>test</p>');
  </script>
  <h3>multiArgument</h3>
  <p>test</p>
</body>

關(guān)于document.write()怎么在JavaScript中使用問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站欄目:document.write()怎么在JavaScript中使用-創(chuàng)新互聯(lián)
當(dāng)前URL:http://muchs.cn/article20/dsipjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站收錄電子商務(wù)、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)