【问题标题】:Assign unique ids to <div> inside <c:forEach>在 <c:forEach> 中为 <div> 分配唯一的 id
【发布时间】:2013-03-21 18:30:44
【问题描述】:

我想为&lt;c:forEach&gt; 中的&lt;div&gt; 分配一个唯一的ID。每当呈现页面时,由&lt;c:forEach&gt; 生成的所有&lt;div&gt;s 都具有相同的ID。有没有办法为&lt;c:forEach&gt;生成的所有div分配唯一的id?我尝试过使用&lt;ui:repeat&gt;,但我遇到了问题,所以我决定坚持使用&lt;c:forEach&gt;

小脸:

<c:forEach var="p" items="#{statusBean.statusList}">
    <h:form>
        <div class="status">
            // Content
        </div>
    </h:form>
</c:forEach>

【问题讨论】:

    标签: jsf jsf-2 jstl


    【解决方案1】:

    使用c:forEach 属性varStatus 定义将包含the status of the loop 的变量。然后你可以像这样在你的模板中使用它:

    <c:forEach var="p" items="#{statusBean.statusList}" varStatus="loop">
            <h:form>
                <div class="status_#{loop.count}">
                // Content
                </div>
            </h:form>
    </c:forEach>
    

    如果你想用 0 启动它,你也可以使用#{loop.index}

    【讨论】:

    • 哦...这比我粗鲁的回答好多了!
    • @jahroy 我也这么认为,但我最初的想法和你一样:“为什么那不是 panelGroup?” :)
    【解决方案2】:

    尝试使用&lt;h:panelGroup&gt; 或其他 JSF 组件来呈现您的 div。

    这将导致为您生成唯一 ID。

    【讨论】:

    • 我如何事先知道 div 的 id,因为我想在 javascript 中使用 id。
    • 如果没有看到你的代码并且知道你想做什么,这是不可能回答的。通常,您会查看生成的 HTML(使用调试器)并弄清楚如何操作它。作为记录,我认为@elias 的答案比我的要好得多。根据他的建议,您将对 ID 有很大的控制权。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    相关资源
    最近更新 更多