以前我自己也犯過這么個(gè)錯(cuò)誤,就是在document.write之后,發(fā)現(xiàn)腳本報(bào)錯(cuò)誤。當(dāng)然后來(lái)是知道了,最近看到有網(wǎng)友在群里提出類似的問題。我在這里就總結(jié)下document.write的用法和注意事項(xiàng)。

          先看下面一個(gè)小網(wǎng)頁(yè):

          <! DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01//EN"?"http://www.w3.org/TR/html4/strict.dtd" >
          < html >
          ????
          < head >
          ????????
          < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=iso-8859-1" ? />
          ????????
          < title > Untitled?Document </ title >
          ????
          </ head >
          ????
          < script? type ="text/javascript" >
          ????????
          function ?InitPage()
          ????????{
          ????????????
          var ?obj? = ?document.getElementById( " DIV_top " );
          ????????????
          // document.write(document.getElementById("DIV_top").style.width);
          ???????????? // document.write(document.getElementById("DIV_top").style.height);
          ????????????document.write(obj.style.width);
          ????????????document.write(obj.style.height);
          ????????????
          ????????}
          ????
          </ script >
          ????
          < body? onload ="InitPage();" >
          ????????
          < div? id ="DIV_top" ?style ="width:400px;?height:200px;" > ?This?is?a?test?! </ div >
          ????
          </ body >
          </ html >

          這個(gè)網(wǎng)頁(yè)執(zhí)行是沒有問題的。

          下面請(qǐng)將函數(shù)當(dāng)前注釋去掉,最后兩行加上注釋,如:

          var?obj?=?document.getElementById("DIV_top");
          ????????????document.write(document.getElementById(
          "DIV_top").style.width);
          ????????????document.write(document.getElementById(
          "DIV_top").style.height);
          ????????????
          //document.write(obj.style.width);
          ????????????//document.write(obj.style.height);

          ???執(zhí)行過程發(fā)生什么情況了? 首先結(jié)果是只輸出了 400px ,如果你的調(diào)試工具夠先進(jìn)就看到了,明確提示document.write(document.getElementById("DIV_top").style.height); 中,document.getElementById("DIV_top"). 為null

          根據(jù)上面的小測(cè)試,我想你對(duì)document.write的細(xì)節(jié)已經(jīng)清楚了。

          ?現(xiàn)在總結(jié)下:
          document.write 產(chǎn)生一個(gè)文檔輸出流,會(huì)將先前的內(nèi)容一次覆蓋。 但是內(nèi)存并沒有釋放對(duì)象,只是頁(yè)面已經(jīng)沒有其句柄。 這個(gè)和js垃圾回收機(jī)制有關(guān)。

          也許你會(huì)問,為什么第一個(gè)例子中的第二句 document.write沒有將第一句的產(chǎn)生的內(nèi)容沖掉呢?

          這個(gè)。。。就好比你以流方式打開一個(gè)文件,第一次寫的時(shí)候是覆蓋,將文件內(nèi)容都清掉。而第二次寫的時(shí)候,并不需要重新打開了。

          試試下面這個(gè)例子:

          <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01//EN">
          <html>
          ????
          <head>
          ????????
          <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
          ????????
          <title>Untitled?Document</title>
          ????
          </head>
          ????
          <script?type="text/javascript">
          ????????
          function?InitPage()
          ????????{
          ????????????
          var?obj?=?document.getElementById("DIV_top");
          ????????????
          var?temp?=?"add";
          ????????????
          ????????????
          var?str?=?"<input?type=\"button\"?onclick=\"document.write(\'可以不停的增加\');\"?name=\'Test\'?value=\'Test\'/>";
          ????????????document.write(obj.style.width);
          ????????????document.write(obj.style.height);
          ????????????document.write(str);
          ????????}

          ????
          </script>
          ????
          <body?onload="InitPage();">
          ????????
          <div?id="DIV_top"?style="width:400px;?height:200px;">?This?is?a?test?!</div>
          ????????
          ????
          </body>
          </html>


          呵呵,比看枯燥的文字有意思吧。程序就是要多動(dòng)手練習(xí),實(shí)踐。特別是自己有想法的時(shí)候,用代碼去證明。

          posted on 2008-11-25 01:41 -274°C 閱讀(5419) 評(píng)論(4)  編輯  收藏 所屬分類: web前端


          FeedBack:
          # re: 說說javascript write函數(shù)
          2009-03-15 14:50 | cht
          程序就是要多動(dòng)手練習(xí),實(shí)踐。特別是自己有想法的時(shí)候,用代碼去證明。
          說得好!支持!  回復(fù)  更多評(píng)論
            
          # re: 說說javascript write函數(shù)
          2010-09-23 08:40 | laptop
          good, 正遇到這個(gè)問題,謝了 樓主  回復(fù)  更多評(píng)論
            
          # re: 說說javascript write函數(shù)
          2013-11-13 14:06 | gloria
          解釋的部分一般,但問題的現(xiàn)象闡述明白了  回復(fù)  更多評(píng)論
            
          # re: 說說javascript write函數(shù)
          2013-11-13 14:06 | gloria

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊(cè)

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 917240
          • 排名 - 40

          最新評(píng)論

          主站蜘蛛池模板: 微博| 德州市| 宁河县| 海南省| 苏尼特左旗| 苍梧县| 渑池县| 黎城县| 黄梅县| 景东| 崇义县| 九龙坡区| 侯马市| 江陵县| 徐州市| 随州市| 兖州市| 华池县| 双峰县| 团风县| 全椒县| 江口县| 琼结县| 彰武县| 巴林右旗| 时尚| 莱西市| 武义县| 绥德县| 阳江市| 黑河市| 威信县| 洛扎县| 绍兴市| 军事| 徐闻县| 麦盖提县| 安宁市| 左云县| 乐东| 全南县|