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

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

          <! 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)頁執(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ì)象,只是頁面已經(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 閱讀(5410) 評(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

          搜索

          •  

          積分與排名

          • 積分 - 914173
          • 排名 - 40

          最新評(píng)論

          主站蜘蛛池模板: 尚志市| 辽阳县| 通化县| 荣昌县| 云梦县| 财经| 达州市| 苏尼特左旗| 虎林市| 五莲县| 佛冈县| 金塔县| 长白| 垣曲县| 长治市| 九江县| 义马市| 牡丹江市| 绥滨县| 磐石市| 双江| 建水县| 新化县| 都昌县| 三都| 淳化县| 曲阜市| 平定县| 特克斯县| 贵德县| 枣阳市| 黄浦区| 凤翔县| 清徐县| 大邑县| 沙湾县| 延吉市| 青州市| 新邵县| 察雅县| 玛曲县|