简体   繁体   中英

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

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM