淘汰赛JS - CSS与类名短跑绑定(Knockout JS - CSS Binding wit

2019-07-29 02:35发布

我有当条件为true的数据绑定淘汰赛申请一个CSS类。 当我在类名中使用破折号(如测试类),那么我得到一个JavaScript错误。

下面是一个说明该问题的小提琴: http://jsfiddle.net/sgvem/2/

<p data-bind="text: property, css: { with-dash: property().length > 0 }"></p>

有没有一种方法来添加一个类用JS淘汰赛几许?

Answer 1:

只要把它放在引号:

<p data-bind="text: property, css: { 'with-dash': property().length > 0 }"></p>

这里有一个更新的小提琴 。

作为一个侧面说明,你不需要> 0 ,因为一个length0将评估为false ,和任何其他长度将评估为true

<p data-bind="text: property, css: { 'with-dash': property().length }"></p>


Answer 2:

您可以限定名称使用'

像这样:

<p data-bind="text: property, css: { 'with-dash': property().length > 0 }"></p>

你的小提琴, 更新

下面是淘汰赛文档解释了CSS绑定: http://knockoutjs.com/documentation/css-binding.html



文章来源: Knockout JS - CSS Binding with dash in class name