knockoutJs 报Uncaught TypeError: Cannot read property 'nodeType' of null错误解决方案

 

         用knockoutJs与第三方UI插件绑定,这个问题困扰了我好几天,头都大了,经过向群里一位大神讨教,终于实现了knockoutJs与easyui的dataGrid 绑定,喜若狂啊喜若狂!

 

        But!

就在这前一刻,我还在为这个错误挠头抓耳:

经过谷歌,终于在http://stackoverflow.com/questions/9128015/knockout-syntax-error找到了解决方案:

 

The problem happened because I was trying to bind a HTML element before it was created.

My script was loaded on top of the HTML and it needs to be loaded at the bottom of my HTML code.

Thanks for your attention James Allardice.

A possible workaround is using defer="defer"

<script src="script.js" type="text/javascript" defer="defer"></script>

Use this if the script is not going to generate any document content. This will tell the browser that it can wait for the content to be load before loading the script.

Further reading.

Hope it helps.

 

        问题原因:ko的viewmodel 加载的时候 ,所绑定的html   DOM 节点还没有加载出来
        两种解决方案:
               1、一是把js 放到要加载的节点后面 加载  
               2、二是在<script>标签里加上defer="defer",defer能够使script脚本等待页面加载完成后再执行
                    < script src = "script.js" type = "text/javascript" defer = "defer" ></ script >
                  
          此外,这个错误在chrome浏览器上调试报成这样,但是在火狐浏览器调试却是报
     
          虽然所报错误和chrome的是在同一行,但是描述不一样,个人认为,chrome浏览器的错误描述更好点。

你可能感兴趣的:(html,js,UI,KnockoutJS)