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 回答