我有一个可观察对象的数组

question = {
        ownerUserName: item.id,
        text: item.text,
        dataType: item.dataType,
        personalized: item.personalized,
        status: item.status,
        actionUserName: item.actionUserName
    }

并从此数组中选择选项:
<select id="questId" style="width: 425px" data-bind="options: questionList, optionsText: 'text'">

我如何才能借助 knockout 法使问题(question.personalized)==“Y”的问题文本的颜色变为绿色?

最佳答案

最好的选择是css binding

根据您的需要快速修改文档将是

<div data-bind="text: personalized, css: personalizedStatus">
   Profit Information
</div>

<script type="text/javascript">
    question.personalizedStatus = ko.computed(function() {
        return this.personalized() == "Y" ? "green" : "red";
    }, question);

</script>
<style>
    .green {color:green;}
    .red{color:red;}
</style>

关于javascript - knockout 选项有条件的CSS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16060021/

10-10 02:27