You need to enable JavaScript to run this app. jQuery实现:li内aaProfileDataWrapper中p为空时隐藏整个li块
优惠活动
大模型
产品
解决方案
定价
更多

jQuery实现:li内aaProfileDataWrapper中p为空时隐藏整个li块

需求与问题排查
  • 目标实现效果:当li元素内部、类名为aaProfileDataWrapper的div容器所包裹的p标签无实际有效文本时,隐藏该li的全部内容(包含内部label、校验模块等所有子元素)。
  • 原有代码的三处核心偏差:
    1. 元素定位错误:原代码通过div.next()查找目标p标签,实际目标p是aaProfileDataWrapper的内部子元素,并非紧随其后的兄弟节点
    2. 判断逻辑不严谨:硬编码匹配精确字符串<p></p>,无法兼容p标签内部存在换行、空格等全空白内容的场景
    3. 作用范围错误:原逻辑仅隐藏了aaProfileDataWrapper自身,没有向上追溯对应的li父元素,无法实现整个li块的隐藏
修正实现方案

逐节点遍历所有aaProfileDataWrapper容器,定位内部p标签做空值判断,确认无内容时向上找到所属li执行隐藏:

$(".aaProfileDataWrapper").each(function () {
    const $wrapper = $(this);
    // 定位容器内需要做空值判断的p标签
    const $checkTarget = $wrapper.find("p");
    // 去除文本前后空白,判断是否无实际内容
    const isTargetEmpty = $checkTarget.text().trim() === "";
    // 向上查找最近的li父元素,执行整块隐藏
    if (isTargetEmpty) {
        $wrapper.closest("li").hide();
    }
});
匹配的页面结构参考
<li id="aaProfilePhone">
  <label>Office 2 Phone:</label> 
  <div class="aaProfileDataWrapper">
    <p></p>
  </div>
  <div class="aaInlineValidationWrapper">
    <div class="aaValidationWrapper-Inner">
      <span class="aaInlineValidationIcon"></span>
      <span class="aaValidationTxt"></span>
    </div>
  </div>
</li>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.08 19:24:08