如何拆分<ul>每6个<li>

时间:2017-04-05 05:33:03

标签: html ejs

例如我在这里有我的代码,我想每6个小时添加<ul>

我尝试在if语句中插入ul但它不起作用。是否可以将ul放在if?

之外
<%  if(array4[j].input_type == "radio") { %>

<li class="list-group-item radio-choice" id="chk_<%=array4[j].variable_name%>" value=""><%=array4[j].answer_text%> </li>

<%}%>

1 个答案:

答案 0 :(得分:1)

<ul>
<% 
for(var j = 0, k = 0; j < array4.length; j++) {
  if(array4[j].input_type == "radio") {
%>

<li class="list-group-item radio-choice" id="chk_<%=array4[j].variable_name%>" value=""><%=array4[j].answer_text%> </li>

<% 
    if(k > 0 && !(k%6) && array4.length-1 != j) { 
%>
</ul>
<ul>
<%
    } 
    k++; 
  }
}
%>
</ul>

更容易阅读评论的内容:

// start <ul>
for(var j = 0, k = 0; j < array4.length; j++) {
  if(array4[j].input_type == "radio") {

    // add <li> here

    // if we're not at the beginning of the array
    // and we're on a multiple of 6
    // and we're not at the end of the array
    if(k > 0 && !(k%6) && array4.length-1 != j) { 

      // close one <ul> and open another

    }
    k++;
  }
}
// end <ul>

remainder operator (%)(也称为模数运算符)使这更容易。

相关问题