同一绑定中的 KnockoutJS css 和 class

KnockoutJS css and class in the same binding

尽管 https://knockoutjs.com/documentation/css-binding.html i wasnt able to use css and class together, instead I could use two different css bindings: http://jsfiddle.net/g9sot5qb/

中的部分

是我做错了什么还是文档不准确?

<span class="cls1 cls2" data-bind="text: title,css: {active: active}, class: myClass" ></span>
<span class="cls1 cls2" data-bind="text: title,css: {active: active}, css: myClass" ></span>

class 绑定是一个 Knockout 版本 3.5 功能。

来自 v 3.5 release notes:

The new class binding supports dynamic class strings.
This allows you to use the css and class bindings together to support both methods of setting CSS classes.


您的 jsfiddle 使用的是旧版本。

另请注意,observable active 必须具有 true 值才能应用 css class 'active'

请参阅下面的(可运行)示例,其中 classcss 绑定都处于活动状态。

var viewModel= {
  myClass: ko.observable('test'),
  title: ko.observable('Title'),
  active: ko.observable(true)
};

ko.applyBindings(viewModel);
.test{
  color: red
}

.active {
  font-size: 32px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.0/knockout-min.js"></script>

<span class="cls1 cls2" data-bind="text: title, css: {active: active}, class: myClass" ></span>