You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jQuery为含reportItem子级的reportGroupItem包裹ul标签

解决方案

方案1:修正模板生成逻辑(推荐)

从根源解决问题,调整你的Struts模板逻辑,增加非空判断,有子报表时才输出<ul>包裹内层迭代内容:

<logic:iterate name="ReportGenerationForm" property="reportGroups" id="group">
<li class="reportGroupItem">
    <bean:write name="group" property="code" /> 
    <!-- 仅当分组下存在报表时才输出ul标签 -->
    <logic:notEmpty name="group" property="reports">
        <ul>
            <logic:iterate name="group" property="reports" id="report" indexId="index">
                <li class="reportItem">
                    <bean:write name="report" property="code" />
                </li>
            </logic:iterate>
        </ul>
    </logic:notEmpty>
</li>
</logic:iterate>

直接渲染出来的DOM结构就符合规范,不需要前端二次处理。

方案2:前端jQuery临时修复

如果暂时无法调整后端模板,用以下jQuery代码实现需求:

$(document).ready(function() {
  $('.reportGroupItem').each(function() {
    // 获取当前分组下直接子级的所有reportItem元素
    const $items = $(this).children('.reportItem');
    // 仅当存在子元素时才执行包裹操作
    if ($items.length) {
      $items.wrapAll('<ul></ul>');
    }
  });
});

逻辑说明:

  • 按分组遍历处理,避免将全页面所有reportItem包裹到同一个ul中
  • 用children()方法仅匹配直接子元素,不会误选深层嵌套的li
  • 增加长度判断,没有子项的分组不会生成空ul标签

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 04:36:05