本文介绍了通过 css 绑定,knockout.js 结合动态和静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在knockout.js 中,我们可以为静态类使用css 绑定

<div data-bind="css: {'translucent ': number() 

和动态

<div data-bind="css: color">静态动态css类</div>

我试过 http://jsfiddle.net/tT9PK/1/ 将它组合成类似 >

css: {color, translucent: number() 

同时获取动态类color 和静态translucent,但出现错误.有没有办法做到这一点?

解决方案

可以通过css属性添加动态类,然后通过attr属性添加静态类

静态动态 css 类

一定要向这个绑定添加任何预定义的类attr: { 'class': color }

In knockout.js we can use css binding for static classes

<div data-bind="css: {'translucent ': number() < 10}">static dynamic css classes</div>

and dynamic

<div data-bind="css: color">static dynamic css classes</div>

I've tried http://jsfiddle.net/tT9PK/1/ to combine it in something like

css: {color, translucent: number() < 10}

to get dynamic class color and static translucent at the same time, but I get an error. Is there a way to do that?

解决方案

You can add dynamic class by css property and then add static class by attr property

<div data-bind="attr: { 'class': color }, css: { 'translucent': number() < 10 }">
  static dynamic css classes
</div>

Be sure to add any predefined classes to this binding attr: { 'class': color }

这篇关于通过 css 绑定,knockout.js 结合动态和静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 04:43