当前位置: 首页 > 知识库问答 >
问题:

jQuery搜索文本并隐藏嵌套跨度

笪栋
2023-03-14

我有这段html代码,想象一下我有不同的div和相同的结构:

我基本上是在span“ld-item-title”(在这里可以看到课程01)中搜索文本“2020”,但没有成功。 即使在span“ld-item-component-progress”的第二个类中,它也会继续搜索文本。

我想要获得的是:

if (in span "ld-item-title" there is "2020") {
    hide span "ld-item-component-progress";
}

null

<div class="ld-item-list-item ld-item-list-item-course ld-expandable learndash-incomplete" id="ld-course-list-item-638">
  <div class="ld-item-list-item-preview">
    <a href="link" class="ld-item-name">
      <div class="ld-status-icon ld-status-incomplete"></div> 
        <span class="ld-item-title">Course 01
         <span class="ld-item-components">
          <span class="ld-item-component-progress">0% Completed</span>
          <span class="ld-sep">|</span>          
          <span class="ld-item-component-steps">0/13 Steps</span>
          <span class="ld-sep">|</span>
          <span class="ld-item-component-since">01/06/2020 09:20</span> 
        </span>
      </span>
    </a>
    <!--/.ld-course-name-->

  </div>
  <!--/.ld-course-preview-->
</div>

<div class="ld-item-list-item ld-item-list-item-course ld-expandable learndash-incomplete" id="ld-course-list-item-74">
<div class="ld-item-list-item-preview">

    <a href="link" class="ld-item-name">
        <div class="ld-status-icon ld-status-incomplete"></div>         <span class="ld-item-title">
            Course Design 2020              <span class="ld-item-components">
                                        <span class="ld-item-component-progress" style="display: none;">
                        0% Completed                        </span>
                                                <span class="ld-sep">|</span>
                                                <span class="ld-item-component-steps">
                        0/11 Steps                      </span>
                                                <span class="ld-sep">|</span>
                                                <span class="ld-item-component-since">
                        26/06/2020 15:12                        </span>
                                    </span>
        </span>
    </a> <!--/.ld-course-name-->

</div> <!--/.ld-course-preview-->
</div>

null

这是我在执行类似任务时使用的代码

(function($) {
     $(document).ready(function(){
         //if item preview contains the term "2020"
         $('.ld-item-list-item-preview').each(function(){
                if($(this).is(":contains('2020')")){
                    // hide children div .ld-item-details
                    $(this).children(".ld-item-details").hide();
                }
            });  
     });
    })(jQuery);

这是针对不同的html结构,但逻辑是相同的

共有1个答案

柯瀚玥
2023-03-14

你是说像这样?

你知道ld-item-title包含嵌套跨度的所有文本吗?

如果您只想要那个文本,那么您需要查看DOM文本节点:

null

$(".ld-item-title").each(function() {
  const textNode = $(this)[0].textContent.split("\n")[0]
  console.log(textNode)
  $(this).find(".ld-item-components").toggle(!textNode.includes("2020"))
})
span::after {
  content: "\A";
  white-space: pre;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ld-item-list-item ld-item-list-item-course ld-expandable learndash-incomplete" id="ld-course-list-item-638">
  <div class="ld-item-list-item-preview">
    <a href="link" class="ld-item-name">
      <div class="ld-status-icon ld-status-incomplete">
      </div>
      <span class="ld-item-title">Course 01
        <span class="ld-item-components">
          <span class="ld-item-component-progress">0%</span>
          <span class="ld-sep">|</span>
          <span class="ld-item-component-steps">0/13 Steps</span>
          <span class="ld-sep">|</span>
          <span class="ld-item-component-since">01/06/2020 09:20</span>
        </span>
      </span>
    </a>
    <!--/.ld-course-name-->
  </div>
  <!--/.ld-course-preview-->
</div>
<div class="ld-item-list-item ld-item-list-item-course ld-expandable learndash-incomplete" id="ld-course-list-item-74">
  <div class="ld-item-list-item-preview">
    <a href="link" class="ld-item-name">
      <div class="ld-status-icon ld-status-incomplete"></div>
      <span class="ld-item-title">Course Design 2020
        <span class="ld-item-components">
          <span class="ld-item-component-progress">0% Completed</span>
          <span class="ld-sep">|</span>
          <span class="ld-item-component-steps">0/11 Steps</span>
          <span class="ld-sep">|</span>
          <span class="ld-item-component-since">26/06/2020 15:12</span>
        </span>
      </span>
    </a>
    <!--/.ld-course-name-->
  </div>
  <!--/.ld-course-preview-->
</div>
 类似资料:
  • 问题内容: 我如何在mongodb文档中嵌套文档的地方搜索文档。例如,我有一组私人消息。每条私人消息都有两个嵌套文档- 一个代表发送用户,另一个代表接收使用。两个嵌套文档的格式均为- userID:34343,名称:Joe Bloggs 我希望能够搜索用户发送的所有邮件(例如,搜索发件人用户的嵌套文档)。 我正在使用Java驱动程序。我是否需要创建一个代表嵌套文档的DBObject? 谢谢 问题答

  • 我需要实现一个在我看来更适合SOAP而不是RESTful服务的搜索,因此我正在努力将其表达为RESTendpoint。 公司(companyId) 合同(Contractd,companyId,privilegeGroupId) PrivilegeGroup(privilegeGroupId,privilegeId) 特权(privilegeId) 主键以粗体显示。 FindPrivilegesB

  • 如何获得空数组和美国的结果和

  • 我已经为一个问题挣扎了一段时间,所以我想我应该通过stackoverflow来解决这个问题。 “我的文档类型”有一个标题、一个语言字段(用于筛选)和一个分组id字段(我省略了所有其他字段以保持重点) 搜索文档时,我希望找到包含标题中文本的所有文档。对于每个唯一的分组id,我只需要一个文档。 我一直在关注tophits聚合,从我所看到的情况来看,它应该能够解决我的问题。 对我的索引运行此查询时: 我

  • 问题内容: 我正在尝试使用以下映射搜索文档: 我想在“ naam”,“ omschrijving”等中进行搜索,但也想在嵌套文档“ kenmerken”的动态映射中进行搜索,因此我创建了两个搜索查询,但它们似乎都不起作用。 我应该使用布尔还是过滤器?或两者结合? 我什至靠近吗? 问题答案: 由于需要解决方案,因此我决定创建一个单独的字符串字段,在其中分解“ kenmerken”字段。目前,此方法可

  • 本文向大家介绍jQuery Jsonp跨域模拟搜索引擎,包括了jQuery Jsonp跨域模拟搜索引擎的使用技巧和注意事项,需要的朋友参考一下 效果还不错 就差加上键盘控制了... 代码如下: 以上所述是小编给大家介绍的jQuery Jsonp跨域模拟搜索引擎的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!

  • 主要内容:实例,jQuery hide() 和 show(),实例,实例,实例,jQuery toggle(),实例隐藏、显示、切换,滑动,淡入淡出,以及动画,哇哦! 因为时间是宝贵的,我们提供快捷方便的学习方法。 在小牛知识库,你可以学习需要的知识。 实例 jQuery hide() 简单的jQuery hide()方法演示。 jQuery hide() 另一个hide()实例。演示如何隐藏文本。 jQuery hide() 和 show() 通过 jQuery,您可以使用 hide() 和

  • 我正在 Visual Studio 代码中处理一个打字稿项目,并希望隐藏 (甚至可能是 )文件,使其不出现在文件资源管理器中。 是否可以在文件资源管理器中仅显示文件?