【发布时间】:2015-07-31 15:17:59
【问题描述】:
您好,我们的部分代码使用 underscore.js 库:
{{ _.each(items, function(item) { }}
<li class="">
<a class="title" href="{{= item.Id }}"><h2>{{= item.Name}}</h2></a>
<p>{{= item.ShortDesc}}</p>
</li>
{{ }); }}
将输出:
<li><a><h2>Element 1</h2></a><p>Description of element 1</p></li>
<li><a><h2>Element 2</h2></a><p>Description of element 2</p></li>
...
但我们想要的是:
<li>
<a><h2>Element 1</h2></a><p>Description of element 1</p>
<a><h2>Element 2</h2></a><p>Description of element 2</p>
</li>
<li>
<a><h2>Element 3</h2></a><p>Description of element 3</p>
<a><h2>Element 4</h2></a><p>Description of element 4</p>
</li>
...
基本上,使用_.each 每 2 项填写一次<li>。
请指教。谢谢
【问题讨论】:
-
无论如何你有两个
</li>,产生无效的html。 -
您对第二个 a 和 p 的期望是什么?
-
@MarcB 这是一个错字...我删除了它。
-
你不能把它们放在那里吗?这似乎需要 html,所以为什么不能在 li 中添加额外的元素?
标签: javascript jquery foreach underscore.js