【问题标题】:How to exclude an item from the search?如何从搜索中排除项目?
【发布时间】:2020-07-05 13:27:07
【问题描述】:

我正在搜索项目。在其中搜索值的块由几个元素组成。我需要从搜索中排除最后一项。那些。这样这个元素就不会受到影响,除了其余的。

我试过这样

$('.blog_content_item').not('.last').each(function() {...});

$(".blog_content_item:not('.last')").each(function() {...});

没有帮助。这在语法上都是不正确的。请告诉我如何从搜索中排除“最后一个”元素。谢谢。

$('.blog_content_item').each(function() {
  if($(this).text().toLowerCase().indexOf(value) === -1) {
  ...
  return;
  } else {
  ...
         }             
});
<div class="blog_content_item">
  <div class="first">
  <div class="middle">
  <div class="last">
</div>

【问题讨论】:

    标签: html jquery search


    【解决方案1】:

    这可能对你有用

    示例 HTML

    <div class="blog_content_item">
      <div class="first">First</div>
      <div class="middle">Middle</div>
      <div class="last">Last</div>
    </div>
    

    脚本

    $('.blog_content_item >div:not(:last-child)').each(function () {
        // your awesome stuffs here
        console.log($(this).text());
    })
    

    【讨论】:

      【解决方案2】:

      使用:not() 过滤器和后代选择器

      https://api.jquery.com/descendant-selector/

      一个元素的后代可以是孩子、孙子、 该元素的曾孙等。

      console.log($('.blog_content_item :not(.last)').get())
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      
      <div class="blog_content_item">
        <div class="first"></div>
        <div class="middle"></div>
        <div class="last"></div>
      </div>

      【讨论】:

      • 虽然,你的答案是正确的,你需要添加好的描述。 OP 也在使用:not()。以您建议的方式使用:not() 如何产生正确的结果?在答案中解释一下。
      • 不起作用。 “last”标签仍未从搜索中排除。
      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多