Knockout.js 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

CSS

css绑定是添加或删除一个或多个CSS classDOM元素上。如下代码:

<div data-bind="css: { profitWarning: currentProfit() < 0 }"> 
  Profit Information
</div>
var viewModel = {
    currentProfit: ko.observable(150000)
    // Positive value, so initially we don't apply the "profitWarning" class    
};    
viewModel.currentProfit(-50);    
// Causes the "profitWarning" class to be applied

效果就是当currentProfit小于0的时候,添加profitWarning CSS class到元素上,如果大于0则删除这个CSS class