简体   繁体   English

同一个绑定中的 KnockoutJS css 和 class

[英]KnockoutJS css and class in the same binding

Despite the section in 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/ Despite the section in 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/

Am I doing something wrong or is the documentation inaccurate?我做错了什么还是文档不准确?

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

The class binding is a Knockout version 3.5 feature. class绑定是 Knockout 3.5版功能。

From the v 3.5 release notes :从 v 3.5发行说明

The new class binding supports dynamic class strings.新的 class 绑定支持动态 class 字符串。
This allows you to use the css and class bindings together to support both methods of setting CSS classes.这允许您一起使用 css 和 class 绑定来支持设置 CSS 类的两种方法。


Your jsfiddle is using an older version.您的 jsfiddle 使用的是旧版本。

Also note that the observable active must have a true value in order to get the css class 'active' applied.另请注意, observable active必须具有true值才能应用css class 'active'

See the (runnable) example below, where both a class and css binding are 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM