4

假设我有一个元素,在里面想放一个不定数量的

  • 项目(基于用户的选择)。有没有办法创建一个允许某种 while 循环的 ICanHaz 模板。例如:

        <ul>
         for(i = 0; i < numOfLi; i++)
           <li> {{ stuff }} </li>
        </ul>
    
  • 4

    3 回答 3

    13

    icanhaz(小胡子)确实包含一种循环方式。

    在 JavaScript 中:

    var listOfStuff = {stuff: [ 
                          {key: "1", desc: "First"},
                          {key: "2", desc: "Second"}
                      ]};
    $("#mySelectBox").append(ich.myTemplate(listOfStuff));
    

    在您看来:

    <script id="myTemplate" type="text/html">
      {{#stuff}}
        <option value="{{key}}">{{desc}}</option>
      {{/stuff}}
    </script>
    
    <select id="mySelectBox">
    </select>
    

    {{#stuff}}分隔{{/stuff}}列表。有关详细信息,请查看mustache的Sections部分。

    编辑:如果您使用的是 jQuery 1.9 或更高版本,请务必查看此答案。

    于 2011-12-16T18:24:04.217 回答
    0

    我不确定 iCanHaz,但 John Resig(JQuery 的创建者)在他的博客上发布了这个方法:

    请参阅JavaScript 微模板

    偷偷摸摸的高峰...

    <script type="text/html" id="user_tmpl">
      <% for ( var i = 0; i < users.length; i++ ) { %>
        <li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>
      <% } %>
    </script>
    
    于 2011-08-23T04:17:52.757 回答
    -2

    没有。做不到。您需要动态呈现 html。

    于 2011-08-16T19:40:44.200 回答