我有一些 JSON 数据:
{
"title": "Available Cars",
"names": [{
"name": "Ford",
"image": "./images/ford.png",
"flags": "./images/us.png",
"description": "Make Average Cars",
"detail": [{
"Profile": "Big US company",
"Background": "Bad"
}]
}, {
"name": "BWM",
"image": "./images/bmw.png",
"flags": "./images/gm.png",
"description": "Make Great Cars",
"detail": [{
"Profile": "MediumGermancompany",
"Background": "Good"
}]
}, {
"name": "VW",
"image": "./images/vw.png",
"flags": "./images/gm.png",
"description": "MakeGoodCars",
"detail": [{
"Profile": "LargeGermancompany",
"Background": "Very Bad"
}]
}]
}
还有一个看起来像这样的模板:
<div><ul><li>{title}</li>{#names}<li data-name=\"{name}\"><a href=\"{name}\"> <h4><img src=\"{flags}\">Name :{name}</h4><p>{description}</p></a></li>{~n}{/names}</ul></div></div>
这将创建以下 HTML:
<div id="container">
<div>
<ul>
<li>Available Cars</li>
<li data-name="Ford"><a href="Ford"><h4><img src="./images/us.png">Name :Ford</h4>
<p>Make Average Cars</p></a></li>
<li data-name="BWM"><a href="BWM"><h4><img src="./images/gm.png">Name :BWM</h4>
<p>Make Great Cars</p></a></li>
<li data-name="VW"><a href="VW"><h4><img src="./images/gm.png">Name :VW</h4>
<p>MakeGoodCars</p></a></li>
</ul>
</div>
</div>
我想添加一些逻辑来查看“背景”的值并根据该值在每个 li 下方呈现一个按钮。例如
If "Background" == "Bad"
<button type="button">Button 1</button>
Else If "Background" == "Good"
<button type="button">Button 2</button>
Else If "Background" == "Very bad""
<button type="button">Button 3</button>
我创建了一个 jsfiddle 来显示:
http://jsfiddle.net/carlskii/Dam29/5/
任何指针将不胜感激!