gpt4 book ai didi

javascript - html5 mustache获取文章索引并比较值

转载 作者:行者123 更新时间:2023-11-30 20:58:59 24 4
gpt4 key购买 nike

我有一个 mustache 文件,我正在遍历数组:

var data = {
sales: [
{
name: "Jim Frost",
region: "USA East",
phone: "212-555-1212",
email: "jfrost@acme-travel.com"
},
{
name: "Jan Smith",
region: "USA West",
phone: "310-555-1212",
},
{
name: "Fred Wesley",
phone: "608-555-1212",
email: "fwesley@acme-travel.com"
},
{
name: "Lisa Moore",
region: "USA South",
phone: "315-555-1212",
email: "lmoore@acme-travel.com"
},
{
name: "Jim Dio",
phone: "+ 44 022-555-1212",
email: "jdio@acme-travel.com"
},
{
name: "Charles Watts",
region: "Spain",
email: "cwatts@acme-travel.com"
},
{
name: "Bert Cooper",
region: "Italy",
email: "bcooper@acme-travel.com"
}
]
};

这是标记:

<div>
<section>
{{#data.sales}}
<article class="items">
<div class="region">{{{region}}}</div>
</article>
{{/data.sales}}
</section>
</div>

仅当区域是 USA East 时,我想添加一些特殊样式(如粗体、颜色等)。如果 {{{region}} 具有特定值,我如何检测 article 元素中的这个固有循环?鉴于比较将与我从后端获得的值进行比较,比如 {{myValue}},它在后端手动设置为 USA East

最佳答案

您可以在 data 中添加一个函数这将根据区域值返回正确的类。有点像

data['regionClass'] = function(){
if ( this['region'] == 'USA East' ) {
return "strong green";
}else{
return "";
}
}

然后在 mustache 中你可以做:<div class="region {{regionClass}}">{{{region}}}</div>

关于javascript - html5 mustache获取文章索引并比较值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com