最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS 中document.write()的用法和清空的原因淺析

 更新時(shí)間:2017年12月04日 12:10:55   作者:abloume  
這篇文章主要介紹了JS 中document.write()的用法和清空的原因淺析,需要的朋友可以參考下

可能很多朋友都遇到過這樣的情況,那就是使用document.write()函數(shù)向網(wǎng)頁(yè)中寫內(nèi)容的時(shí)候,會(huì)把文檔中的原來的內(nèi)容給清空,這一點(diǎn)對(duì)于初學(xué)者來說算是一個(gè)困擾,下面就介紹一下為什么會(huì)出現(xiàn)這種情況,當(dāng)然也就知道如何避免此種情況的發(fā)生了。

  先看一段代碼實(shí)例:

<!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)頁(yè)中的內(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有如下提示:

這里寫圖片描述

// 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('<h2>multiArgument</h2>','<p>test</p>');
  </script>
</body>
<!-- 運(yùn)行后 -->
<body>
  <script>
    document.write('<h2>multiArgument</h2>','<p>test</p>');
  </script>
  <h2>multiArgument</h2>
  <p>test</p>
</body>

總結(jié)

以上所述是小編給大家介紹的JS 中document.write()的用法和清空的原因淺析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

闽清县| 诏安县| 罗平县| 老河口市| 上饶市| 都江堰市| 疏勒县| 苗栗市| 平罗县| 神农架林区| 衡东县| 邮箱| 军事| 田阳县| 天峻县| 四子王旗| 宁晋县| 高台县| SHOW| 商南县| 南丹县| 嘉鱼县| 登封市| 射阳县| 金塔县| 明光市| 彰化市| 南岸区| 呈贡县| 曲阳县| 门源| 吉安市| 游戏| 常德市| 张掖市| 桂东县| 资阳市| 宣武区| 友谊县| 宁远县| 星座|