JavaScript runtime error: Unable to get property 'nodeType' of undefined or null reference

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I tried running an application with knockoutjs script included with jquery and my own js file that has a ViewModel actual binding to the controls.

I am getting the exception every time i run the application.

Is this the issue in my system, or the Visual Studio? I even tried running the html file alone in the browser, i couldn't see any exceptions but it stopped me from performing all other functionalists that are expected to be done from knockoutjs.

Please help me in this regard

This is my full code

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title></title>     <script src="Scripts/jquery-1.7.1.min.js"></script>     <script src="Scripts/knockout-2.2.0.js"></script>      <script type="text/javascript">        // Here's my data model         debugger;         function viewModel() {             this.day = ko.observable('24');             this.month = ko.observable('02');             this.year = ko.observable('2012');              this.fullDate = ko.computed(function () {                 return this.day() + "/" + this.month() + "/" + this.year();             }, this);         };          ko.applyBindings(new viewModel());      </script>  </head> <body>     <p>Day:         <input data-bind="value: day" /></p>     <p>Month:         <input data-bind="value: month" /></p>     <p>Year:         <input data-bind="value: year" /></p>     <p>The current date is <span data-bind="text: fullDate"></span></p>   </body> </html> 

回答1:

You called applyBindings before browser rendered html. You have to move script tag to the bottom of the page or put your code to document ready handler:

<script type="text/javascript">  $(function(){      debugger;     function viewModel() {         this.day = ko.observable('24');         this.month = ko.observable('02');         this.year = ko.observable('2012');          this.fullDate = ko.computed(function () {             return this.day() + "/" + this.month() + "/" + this.year();         }, this);     };      ko.applyBindings(new viewModel()); }); </script> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!