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

如何隐藏含指定文本的spanLabel类div?现有JS代码仅在无子元素时生效

Fix: Hide .spanLabel When "end date is empty" Exists (Even With .boldLabel Present)

Got it, let's break down why your current code isn't working first:

Your HTML has the .spanLabel div containing both the bold "END DATE: " span and the "end date is empty" text. When you use $(this).text() on the .spanLabel div, it returns the combined text: END DATE: end date is empty (trimmed). Your condition checks if this equals exactly end date is empty, which only happens when you remove the .boldLabel span—hence why it only works then.

Here are a few reliable solutions to fix this:

Solution 1: Check if the target text exists within the div

If you just need to hide the div whenever "end date is empty" is present (regardless of the leading label), adjust the condition to check for the presence of the target string:

$('.spanLabel').filter(function() {
  // Check if the trimmed text includes our target string
  return $.trim($(this).text()).includes('end date is empty');
}).hide();

Solution 2: Target only the non-bold text content

If you want to be precise (only hide when the text after the bold label is exactly "end date is empty"), extract the text that's not inside the .boldLabel span:

$('.spanLabel').filter(function() {
  // Clone the div, remove the .boldLabel span, then get/trim the remaining text
  const remainingText = $(this).clone().find('.boldLabel').remove().end().text().trim();
  return remainingText === 'end date is empty';
}).hide();

Solution 3: Use a direct text node check

If "end date is empty" is a direct text node inside the .spanLabel div, target it directly:

$('.spanLabel').filter(function() {
  // Loop through child nodes to find the text node with our target content
  const textNodes = Array.from(this.childNodes).filter(node => node.nodeType === Node.TEXT_NODE);
  const targetText = textNodes.map(node => node.textContent.trim()).join('');
  return targetText === 'end date is empty';
}).hide();

All three solutions work without needing to remove the .boldLabel span first. Solution 2 is the most precise for your original HTML structure.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 08:52:44