如何在Underscore.js中使用_.each()遍历每2个项目 {{= item....

问题描述 投票:0回答:2

您好,我们在某些代码中使用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>
...

基本上,每[2]使用<li>填充_.each。>>

请告知。谢谢

嗨,我们正在使用underscore.js库来编写某些代码:{{_.each(items,function(item){}}

  • {{= item....

  • ]
    javascript jquery foreach underscore.js
    2个回答
    2
    投票
    传递给_.each()的回调函数将循环索引或对象键作为其第二个参数,因此从理论上讲您可以使用它。就是说,如果不仔细地做的话,您可能会发现自己最后缺少一个结束的</li>标签。

    0
    投票
    _.each还通过向迭代器函数添加第二个参数为您提供当前迭代的索引,因此您可以执行以下操作:
    © www.soinside.com 2019 - 2024. All rights reserved.