本文介绍了JavaScript运行时错误:无法获取未定义或空引用的属性'nodeType'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试运行一个包含jquery和我自己的js文件的knockoutjs脚本的应用程序,该文件具有ViewModel实际绑定到控件的功能。

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.

当我运行应用程序。

这是我的系统或Visual Studio中的问题吗?我甚至尝试在浏览器中单独运行html文件,我看不到任何异常,但它阻止我执行所有其他的功能主义者,这些功能主义者预计会从knockoutjs中完成。

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.

请在这方面帮助我

这是我的完整代码

<!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>


推荐答案

您在浏览器呈现html之前调用applyBindings。您必须将脚本标记移动到页面底部或将您的代码放置到文档就绪处理程序中:

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>

这篇关于JavaScript运行时错误:无法获取未定义或空引用的属性'nodeType'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:07