Javascript document.getElementById似乎不起作用

时间:2010-07-09 05:33:21

标签: javascript getelementbyid

我有以下代码,Chrome中的javascript控制台说“无法读取null的innerHTML属性。为什么document.getElementById('display')空出来了?

<!DOCTYPE html>
<html>
    <head>
        <title>Chat 3</title>
        <script type="text/javascript">
            function showmsg(str){
                var display = document.getElementById('display');
                display.innerHTML += "<p>" + str + "</p>";
            }

            if("WebSockets" in window){
                pass;
            }else{
                showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
            }
        </script>

        <style type="text/css">
        #username { 
            padding: 0px;
            margin: 0px;
            height: 26px;
            width: 400px;
        }

        /* ADDED container div that wraps onlineusers and display */
        #container {
          margin: 10px 0;
        }

        /* use float: left to put them side-by-side */
        #display { 
            padding: 0px;
            margin: 0px;
            border-style: solid;
            border-width: 1px;
            overflow: auto;
            height: 400px;
            width: 400px;
            float: left;
        }

        #onlineusers { 
            padding: 0px;
            margin: 0px;
            height: 400px;
            width: 200px;
            border-style: solid;
            border-width: 1px;
            float: left;
        }

        /* Added container2 to wrap inputline and sendbutton */
        #container2 {
            margin: 10px 0;
        }

        #inputline { 
            padding: 0px;
            margin: 0px;
            height: 26px;
            width: 350px;
            float: left;
        }

        #sendbutton {
            padding: 0px;
            margin: 0px;
            height: 30px;
            width: 50px;
            float: left;
        }

        /* this is a well used "hack". */
        .clearfix {
          clear: both;
        }
        </style>
    </head>
    <body onload="document.getElementById("username").focus()">
            <input type="text" id="username" />
            <div id="container">
                <div id="display"></div>
                <div id="onlineusers"></div>
                <div class="clearfix"></div>
            </div>

            <div id="container2">
                <input type="text" id="inputline" length="55" />
                <input type="button" id="sendbutton" value="Send" />
            </div>
    </body>
</html>

3 个答案:

答案 0 :(得分:7)

将脚本部分向下移动到页面底部。否则,它将在页面加载之前执行。

顺便说一句:将脚本放在页面底部甚至是“最佳做法”,并由Google推荐Yahoo&amp;共

答案 1 :(得分:6)

您在整个页面之前调用showmsg(因此加载了display div)。因此错误。从onload调用它,它将起作用。

将此功能添加到<head>

function handleLoad()
{
  document.getElementById("username").focus()
  if("WebSockets" in window){
   pass;
  }else{
    showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
  }
}

并从onload

调用它
<body onload="handleLoad()">

答案 2 :(得分:2)

将脚本标记放在文档的末尾: 在您的情况下,页面尚未完全解析为DOM树,但您的脚本仍然执行:

       <body onload="document.getElementById('username').focus()">
           <input type="text" id="username" />
           <div id="container">
               <div id="display"></div>
               <div id="onlineusers"></div>
               <div class="clearfix"></div>
           </div>

           <div id="container2">
               <input type="text" id="inputline" length="55" />
               <input type="button" id="sendbutton" value="Send" />
           </div>

           <script type="text/javascript">
           function showmsg(str){
               var display = document.getElementById('display');
               display.innerHTML += "<p>" + str + "</p>";
           }

           if("WebSockets" in window){
              pass;
           }else{
               showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
           }
       </script>
   </body>

出于性能原因,这也被认为是一种很好的做法: http://developer.yahoo.com/performance/rules.html#js_bottom