3

我有以下代码,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>
4

3 回答 3

7

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

顺便说一句:将脚本放在页面底部甚至是一种“最佳实践”,并受到 Google、Yahoo & Co的推荐

于 2010-07-09T05:41:32.937 回答
6

您在加载showmsg整个页面(以及因此 div)之前调用。display因此错误。调用它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()">
于 2010-07-09T05:36:30.827 回答
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

于 2010-07-09T05:40:49.530 回答