0

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/

4

1 に答える 1

2

値をアンラップする必要があり$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>​
于 2012-11-23T14:23:03.377 に答える