<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    以前我自己也犯過這么個錯誤,就是在document.write之后,發現腳本報錯誤。當然后來是知道了,最近看到有網友在群里提出類似的問題。我在這里就總結下document.write的用法和注意事項。

    先看下面一個小網頁:

    <! 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 >

    這個網頁執行是沒有問題的。

    下面請將函數當前注釋去掉,最后兩行加上注釋,如:

    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);

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

    根據上面的小測試,我想你對document.write的細節已經清楚了。

    ?現在總結下:
    document.write 產生一個文檔輸出流,會將先前的內容一次覆蓋。 但是內存并沒有釋放對象,只是頁面已經沒有其句柄。 這個和js垃圾回收機制有關。

    也許你會問,為什么第一個例子中的第二句 document.write沒有將第一句的產生的內容沖掉呢?

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

    試試下面這個例子:

    <!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>


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

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


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

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網站

    關注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914354
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 久久国产美女免费观看精品| 免费看片在线观看| 亚洲国产成人久久精品动漫| 青娱分类视频精品免费2| 美女免费视频一区二区三区| 久久伊人久久亚洲综合| 午夜一级免费视频| a级毛片毛片免费观看久潮| 亚洲五月丁香综合视频| 久久久久亚洲AV无码专区网站| 99久久久国产精品免费牛牛四川 | 在线观看亚洲天天一三视| 免费A级毛片av无码| 亚洲精品无码久久久久久| 亚洲国产精品一区第二页| 国产精品久久免费视频| 中国xxxxx高清免费看视频| 色吊丝免费观看网站| 亚洲妓女综合网99| 亚洲午夜久久久久久久久电影网| 欧美三级在线电影免费| 国产99视频精品免费视频76| 毛片无码免费无码播放| 一级特黄aaa大片免费看| 亚洲一区二区三区乱码在线欧洲| 亚洲香蕉网久久综合影视| 拔擦拔擦8x华人免费久久| 2019中文字幕在线电影免费 | 免费va人成视频网站全| 无码av免费毛片一区二区| 野花香高清视频在线观看免费| 无码亚洲成a人在线观看| 亚洲国产午夜精品理论片| 亚洲国产另类久久久精品小说| 国产性生交xxxxx免费| 毛片视频免费观看| 亚洲视频在线观看免费视频| 免费国产成人α片| 久久嫩草影院免费看夜色| 一级毛片人与动免费观看| 久久精品国产亚洲AV电影网|