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

本文解决了KnockoutJS与第三方UI插件EasyUI的dataGrid绑定问题,介绍了导致错误的原因及两种解决方法:调整脚本加载位置或使用defer属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

         用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浏览器的错误描述更好点。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值