gpt4 book ai didi

javascript - knockoutJS $index 无法正常工作

转载 作者:行者123 更新时间:2023-12-02 19:12:31 28 4
gpt4 key购买 nike

JS

 function NotificationsViewModel()
{
this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'];
this.activeMonth = ko.observable(10);
}

ko.applyBindings(new NotificationsViewModel());

HTML

<ul data-bind="foreach: months">
<li data-bind="css:{'selected-month': $index == $root.activeMonth()}">
<span data-bind="text: $index"></span> : <span data-bind="text: $data"></span>
</li>
</ul>​

http://jsfiddle.net/fhZph/

最佳答案

您应该解开 $index 值:

<ul data-bind="foreach: months">
<li data-bind="css:{'selected-month': $index() == $root.activeMonth()}">
<span data-bind="text: $index"></span> : <span data-bind="text: $data"></span>
</li>
</ul>​

关于javascript - knockoutJS $index 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13530900/

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