我使用了knockout js的示例,它不起作用。我不知道为什么点击事件没有触发。

HTML:

<div>
    You've clicked <span data-bind="text: numberOfClicks"></span> times
    <button data-bind="click: incrementClickCounter">Click me</button>
</div>


Javascript:

<script type="text/javascript">
    var viewModel = {
    numberOfClicks : ko.observable(0),
    incrementClickCounter : function() {
    alert("hi im click");
    var previousCount = this.numberOfClicks();
    this.numberOfClicks(previousCount + 1);
     }
    };
</script>

最佳答案

试试这个代码
通过link

HTML:

<div>
    You've clicked <span data-bind="text: numberOfClicks"></span> times
    <button data-bind="click: incrementClickCounter">Click me</button>
</div>


Javascript:

<script src="~/Scripts/jquery-2.1.1.js"></script>
<script src="~/Scripts/knockout-3.2.0.js"></script>
<script type="text/javascript">
    var viewModel = {
    numberOfClicks : ko.observable(0),
    incrementClickCounter : function() {
    alert("hi im click");
    var previousCount = this.numberOfClicks();
    this.numberOfClicks(previousCount + 1);
     }
    };

ko.applyBindings(new viewModel());
</script>

09-25 17:41