我是淘汰新手,在同一页面上创建多个“喜欢”按钮计数器时遇到问题。
这是我的代码。
<ul>
<li>
<h4>Item 1</h4>
<div><span data-bind='text: numberOfClicks1'> </span> likes</div>
<button data-bind='click: registerClick'>Like?</button>
</li>
<li>
<h4>Item 2</h4>
<div><span data-bind='text: numberOfClicks2'> </span> likes</div>
<button data-bind='click: registerClick'>Like?</button>
</li>
</ul>
<script type='text/javascript' src='js/knockout-2.2.1.js'></script>
<script type="text/javascript">
var item1 = function() {
this.numberOfClicks1 = ko.observable(0);
this.registerClick = function() {
this.numberOfClicks1(this.numberOfClicks1() + 1);
};
};
ko.applyBindings(new item1());
var item2 = function() {
this.numberOfClicks2 = ko.observable(0);
this.registerClick = function() {
this.numberOfClicks2(this.numberOfClicks2() + 1);
};
};
ko.applyBindings(new item2());
</script>
不确定我需要做什么?