gpt4 book ai didi

javascript - knockout js点击绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-11-30 12:37:37 24 4
gpt4 key购买 nike

我使用了 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>

关于javascript - knockout js点击绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com