淘汰赛JS-同时使用静态类名和数据绑定类名 [英] Knockout JS - Use both static class name as well as data bound class name
问题描述
有人知道我是否可以使用KnockoutJS同时具有静态类名和动态数据绑定类名的html元素?像这样:
Anyone know if I can have an html element with both a static class name as well as a dynamic, data-bound class name using KnockoutJS? Something like this:
<span class='staticClassName {{viewModelPropertyValue}}'></span>
我意识到这不是ko语法,我只是在使用这种语法来说明要点.
I realize this isn't ko syntax, I'm just using this syntax to get the point across.
推荐答案
您可以使用css
绑定基于值的真实性添加和删除类,但是听起来您的viewModelProperty是a的名称类而不是布尔值.
You can use the css
binding to add and remove a class based on the truthiness of a value, but it sounds like your viewModelProperty is the name of a class rather than a boolean.
您可以将attr
绑定与包含的静态类一起使用,例如:(attr: { 'class': 'staticClassName ' + viewModelPropertyValue }
或(viewModelPropertyValue()
,如果可以观察到的话).)
You can use the attr
binding with the static class included like: (attr: { 'class': 'staticClassName ' + viewModelPropertyValue }
or (viewModelPropertyValue()
if it is an observable).
否则,这里有一个社区绑定,它将按照您遵循的方式添加和删除类:
Otherwise, there is a community binding here that will add and remove a class in the manner that you are after: https://github.com/SteveSanderson/knockout/wiki/Bindings---class
这篇关于淘汰赛JS-同时使用静态类名和数据绑定类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!