入门

监控属性

使用内置绑定

控制文本和外观

绑定逻辑控制

处理表单属性

解析模板

高级应用

插件

更多信息

"css" 绑定

目的

使用 css 绑定在元素中添加或删除一个或多个的CSS类。 例如,当出现某些值变为负数是需要突出是这绑定非常有用。

css binding adds or removes one or more named CSS classes to the associated DOM element. This is useful, for example, to highlight some value in red if it becomes negative.

(注: 如果你不想指定一个CSS类,而是需要指定一个样式请使用 style 绑定,请查看 style 绑定。)

(Note: If you don’t want to apply a CSS class but instead want to assign a style attribute value directly, see the style binding.)

例子

  1. <div data-bind="css: { profitWarning: currentProfit() < 0 }">
  2. Profit Information
  3. </div>
  4. <script type="text/javascript">
  5. var viewModel = {
  6. currentProfit: ko.observable(150000) // Positive value, so initially we don't apply the "profitWarning" class
  7. };
  8. viewModel.currentProfit(-50); // Causes the "profitWarning" class to be applied
  9. </script>

currentProfit值大于0时 profitWarning CSS样式将不会被应用,小于0时CSS将被类将被应用在DOM中。

This will apply the CSS class profitWarning whenever the currentProfit value dips below zero, and remove that class whenever it goes above zero.

参数

  • 主参数

在对象属性中传入一个CSS类名,如果表达式的值为 true 时CSS类将被应用,如果值为 false 时样式将会被移除。

You should pass a JavaScript object in which the property names are your CSS classes, and their values evaluate to true or false according to whether the class should currently be applied.

你可以一次设置多个CSS类。 例如, 如果你的 view model 中有一个属性为 isSevere

You can set multiple CSS classes at once. For example, if your view model has a property called isSevere,

  1. <div data-bind="css: { profitWarning: currentProfit() < 0, majorHighlight: isSevere }">

非布尔值将被解释为布尔值。 例如, 0null 都会被解释为 false,而 21 和 非null 对象都会解释为 true

Non-boolean values are interpreted loosely as boolean. For example, 0 and null are treated as false, whereas 21 and non-null objects are treated as true.

如果参数中有任何 observable 值, 当值发生改变是将会对应添加删除更改。如果参数中不包含 observable 值,绑定将会只应用一次。

If your parameter references an observable value, the binding will add or remove the CSS class whenever the observable value changes. If the parameter doesn’t reference an observable value, it will only add or remove the class once and will not do so again later.

像其他绑定一样,参数中你可以任意使用传入JavaScript表达式。

As usual, you can use arbitrary JavaScript expressions or functions as parameter values. KO will evaluate them and use the resulting values to detemine whether to apply the CSS class or remove it.

  • 附加参数

注: 参数中使用合法的JavaScript的变量

如果你想应用的类名叫my-class,你不能这样写:

If you want to apply the CSS class my-class, you can’t write this:

  1. <div data-bind="css: { my-class: someValue }">...</div>

… 因为 my-class 不是合法的标识名。解决方法和简单: 使用引号将标识包裹起来,让他成为一个字符串。 这是一个JavaScript对象直面量表示法 (技术上根据JSON归法你应该始终做到这一点)。例如,

… because my-class isn’t a legal identifier name at that point. The solution is simple: just wrap the identifier name in quotes so that it becomes a string literal. This is legal in a JavaScript object literal (technically, according to the JSON spec, you should always do this anyway, though in practice you don’t have to). For example,

  1. <div data-bind="css: { 'my-class': someValue }">...</div>

依赖性

除KO核心类库外,无依赖。

(c) knockoutjs.com