2014-12-05 80 views
1

裏面我有我的木偶視圖中使用的HTML模板。當我在渲染函數中調用它時,我在模板中傳遞了一個參數。我想根據參數的值添加多個div元素。如何在html模板中添加for循環?如何爲循環使用HTML模板

<div id="mainArea"> 
 
    <div data-role="view"> 
 
     <div id="scrollView" data-role="scrollView" data-stretch="true"> 
 
      <div id = 'leftArrowImage' class="leftArrowImage"> 
 
      </div> 
 

 
      <div id = 'mainViewArea'></div> 
 
      <div id = 'rightArrowImage'></div> 
 
      
 
      
 
      /* I have passed a variable here named allWidgets. I want to add the number of divs(<div data-role="page" ></div>) equal to the length of allWidgets array. How do I do that*/ 
 
     </div> 
 
    </div> 
 
</div>

+0

?答案取決於你用來構建HTML的內容。 – Erik 2014-12-05 12:29:27

+0

我想用JSTL – 2014-12-05 12:42:04

+0

或者使用scriplets – 2014-12-05 12:59:12

回答

1

<link rel="stylesheet" type="text/css" href="Widgets/ReportWidget/styles/scrollLayout.css"/> 
 

 
<div id="mainArea"> 
 
    <div data-role="view"> 
 
     <div id="scrollView" data-role="scrollView" data-stretch="true"> 
 
      <div id = 'leftArrowImage' class="leftArrowImage"> 
 
      </div> 
 

 
      <div id = 'mainViewArea'></div> 
 
      <div id = 'rightArrowImage'></div> 
 
      <% var i; 
 
      for i in allWidgets{ %> 
 
       <div data-role="page" class="pages" > 
 
      </div> 
 
      }%> 
 
     </div> 
 
    </div> 
 
</div>

您正在使用的編程語言
+0

我認爲這應該幫助 – 2014-12-08 06:50:01

0

如果你在談論(我不知道說明白的問題完全一致),你可以試試這個;

<div id="mainArea"> 
    <div data-role="view"> 
     <div id="scrollView" data-role="scrollView" data-stretch="true"> 
      <div id = 'leftArrowImage' class="leftArrowImage"> 
      </div> 
      <div id = 'mainViewArea'></div> 
      <div id = 'rightArrowImage'></div> 


      <c:forEach var="widget" items="${allWidgets}"> 
       // Here, you can add parameters about widget objects in allWidgets 
       <div data-role="page" ></div> 
      </c:forEach> 
     </div> 
    </div> 
</div> 

還必須定義下面的代碼你的JSP文件的頂部,

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
+0

你嘗試這種添加代碼??? – 2014-12-05 23:04:38

+0

沒關係把它只是我要使用循環使用擴展的.html文件中。我怎麼做?? – 2014-12-08 06:40:39

+0

您必須使用javascript才能在.html中使用循環。這個問題可以幫助你:http://stackoverflow.com/questions/16554355/javascript-to-loop-increment-html-code-with-values-up-to-55 – 2014-12-08 09:32:00