无法更改div的内容 - 未捕获TypeError:无法设置null的属性'innerHTML'

时间:2013-09-29 01:49:49

标签: javascript jquery html5 dom

我想使用javascript和innerHTML更改div的内容。

我无法让它发挥作用。我刚刚添加了更改div的代码,之前代码工作正常。我仔细检查了语法。

我正在使用webshims,openlayers和jquery / javascript

在控制台中,我看到了

Uncaught TypeError: Cannot set property 'innerHTML' of null imagegaledit - myFile.php :768 so.onmessage - myFile.php :324

768就是那条线 document.getElementById("imgedtitle").innerHTML=mnmi[0];

和324就是这个 imagegaledit(0);

帮助不大? 感谢

修改 websockets工作和反应很好

这是代码(简明扼要)

    <!doctype html>

    <header>    
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html">

    <script src="jquery-1.8.2.min.js"></script> 
    <script src="js-webshim/minified/extras/modernizr-custom.js"></script>  
    <script src="js-webshim/minified/polyfiller.js"></script>
    <script>    
        $.webshims.polyfill();  
    </script>

    <script src="http://maps.google.com/maps/api/js?sensor=false"></script>

    <!--open layers api library-->
    <script type='text/javascript' src='OpenLayers.js'></script>
    <script type='text/javascript'>

    //openlayers vars and stuff here...

    function init(){

       //when a point on map clicked...
       function selected_feature(event){
             //some openlayers magic...
              var so = new WebSocket("ws://localhost:8000");         

              so.onerror=function (evt) 
               {response.textContent = evt;}

                so.onopen = function(){
                response.textContent = "opened";
                so.send(JSON.stringify({command:'map',fmid:jas}));
                 }


                so.onmessage = function (evt) {    
                  var received_msg = evt.data;
                  var packet = JSON.parse(received_msg); 
                  //pass data to var and arrays...                       
                 imagegaledit(0);
                }
          }//closes function selected_feature

    }//closes init

    function imagegaledit (w){
        if (w==0){
           document.getElementById("imgedtitle").innerHTML=mnmi[0];
          }
    }//closes imagegaledit 


</script>    

    <body onload='init();'>
        Title</br><div id="imgedtitle"> </div> </br>
    </body>

1 个答案:

答案 0 :(得分:0)

您需要一个结束脚本标记:

</script> 
<body>