Knockout JS - CSS在类名中用短划线绑定 [英] Knockout JS - CSS Binding with dash in class name
问题描述
如果条件成立,我在Knockout中有一个数据绑定来应用CSS类。当我在类名中使用短划线(例如test-class)时,我得到一个javascript错误。
I have a data binding in Knockout to apply a CSS class if a condition is true. When I use a dash in the class name (such as test-class) then I get a javascript error.
这是一个演示问题的小提琴: http://jsfiddle.net/sgvem/2/
Here is a fiddle that demonstrates the problem: http://jsfiddle.net/sgvem/2/
<p data-bind="text: property, css: { with-dash: property().length > 0 }"></p>
有没有方法使用Knockout JS添加一个破折号的类?
Is there a way to add a class with a dash using Knockout JS?
推荐答案
只需将它放在引号中:
Just put it in quotes:
<p data-bind="text: property, css: { 'with-dash': property().length > 0 }"></p>
以下是更新的小提琴。
另外,您不需要>因为
0
的一个长度
将评估为 false <0> / code>,其他任何长度都会评估为
true
:
As a side note, you don't need the > 0
since a length
of 0
will evaluate to false
, and any other length will evaluate to true
:
<p data-bind="text: property, css: { 'with-dash': property().length }"></p>
这篇关于Knockout JS - CSS在类名中用短划线绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!