其他分享
首页 > 其他分享> > 我无法获取最简单的基因敲除示例工作?

我无法获取最简单的基因敲除示例工作?

作者:互联网

我已经尝试过此代码,但无法正常工作.我的代码如下

<link href="~/Content/knocktest.css" rel="stylesheet" />
    <script src="~/Scripts/knockout-2.3.0.js" type="text/javascript"></script>

<script type="text/javascript">
        $(document).ready(function () {
            var ViewModel = function (first, last) {

                this.firstName = ko.observable(first);
                this.lastName = ko.observable(last);

                this.fullName = ko.computed(function () {                  

                    return this.firstName() + " " + this.lastName();
                }, this);
            };
            ko.applyBindings(new ViewModel("Planet", "Earth"));
        });
    </script>

我下面的html代码

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>

解决方法:

您的代码中没有错误,所以实际上唯一缺少的是自使用以来对jQuery库的引用.

$(document).ready(function () {
    // rest of your code here
});

如果您不包括jQuery,则可以删除$(document).ready()代码,并确保您的JavaScript位于正文中所有html元素之后.

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>

<span data-bind="text: firstName"></span>
<span data-bind="text: lastName"></span>

<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"> </script>
<script>

    var ViewModel = function (first, last) {

        this.firstName = ko.observable(first);
        this.lastName = ko.observable(last);

        this.fullName = ko.computed(function () {
            return this.firstName() + " " + this.lastName();
        }, this);

    };
    ko.applyBindings(new ViewModel("Planet", "Earth"));

</script>

请检查这个demo of your code at jsbin

标签:knockout-js,javascript
来源: https://codeday.me/bug/20191122/2063109.html