使用jQuery按照其中的内容过滤标签

时间:2018-06-11 11:10:34

标签: javascript jquery html

我想在<a>代码中进行搜索,只显示符合要求的代码。因此,当您加载页面时,所有<a>标记都是可见的。当您在搜索框中输入内容时,我想按照其中的标题过滤<a>标记。

$('#search').on('keyup', function(e) {
    var search = $(this).val().toLowerCase();
    $('#content').children().children().children("p").each(function() {
        $(this).parent().parent()[search && $(this).text().toLowerCase().match('^' + search) ? 'show' : 'hide']();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" />
    <div class="content">
            <a>
                <div class="box">
                    <img>
                    <p class="title">John</p>
                    Other content.
                </div>
            </a>
            <a>
                <div class="box">
                    <img>
                    <p class="title">Jenny</p>
                    Other content.
                </div>
            </a>
            <a>
                <div class="box">
                    <img>
                    <p class="title">Jane</p>
                    Other content.
                </div>
            </a>
            <a>
                <div class="box">
                    <img>
                    <p class="title">Jack</p>
                    Other content.
                </div>
            </a>
     </div>

上面的javascript不起作用。那么如何按标题过滤?

3 个答案:

答案 0 :(得分:5)

要实现此目的,您可以使用filter()查找不符合搜索要求的.title元素,然后隐藏其父.box。但请注意,对于不区分大小写的搜索,您需要使用自己的逻辑扩展: jQuery运算符,该逻辑是:contains的变体,如下所示:

&#13;
&#13;
jQuery.expr[':']['contains-insensitive'] = function(a, i, m) {
  return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};

var $box = $('.box');
$('#search').on('input', function(e) {
  $box.show();
  $('.title').filter(`:not(:contains-insensitive('${this.value}'))`).closest('.box').hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" />
<div class="content">
  <a>
    <div class="box">
      <img>
      <p class="title">John</p>
      Other content.
    </div>
  </a>
  <a>
    <div class="box">
      <img>
      <p class="title">Jenny</p>
      Other content.
    </div>
  </a>
  <a>
    <div class="box">
      <img>
      <p class="title">Jane</p>
      Other content.
    </div>
  </a>
  <a>
    <div class="box">
      <img>
      <p class="title">Jack</p>
      Other content.
    </div>
  </a>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

#content定位ID为content的元素。您已将content分配给div,其目标应为.content

&#13;
&#13;
$('#search').on('keyup', function(e) {
  var search = $(this).val().toLowerCase();
  $('.content').children().children().children("p").each(function() {
    $(this).parent().parent()[search && $(this).text().toLowerCase().match('^' + search) ? 'show' : 'hide']();
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" />
<div class="content">
  <a>
    <div class="box">
      <img>
      <p class="title">John</p>
      Other content.
    </div>
  </a>
  <a>
    <div class="box">
      <img>
      <p class="title">Jenny</p>
      Other content.
    </div>
  </a>
  <a>
    <div class="box">
      <img>
      <p class="title">Jane</p>
      Other content.
    </div>
  </a>
  <a>
    <div class="box">
      <img>
      <p class="title">Jack</p>
      Other content.
    </div>
  </a>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

以下是工作代码:https://codepen.io/creativedev/pen/PapKjr

$('#search').on('keyup', function(e) {
            var search = $(this).val().toLowerCase();
            $('.content').children().find("p").each(function() {console.log($(this).text());
                $(this).parent().parent()[search && $(this).text().toLowerCase().match('^' + search) ? 'show' : 'hide']();
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" />
    <div class="content">
            <a>
                <div class="box">
                    <img>
                    <p class="title">John</p>
                    Other content.
                </div>
            </a>
            <a>
                <div class="box">
                    <img>
                    <p class="title">Jenny</p>
                    Other content.
                </div>
            </a>
            <a>
                <div class="box">
                    <img>
                    <p class="title">Jane</p>
                    Other content.
                </div>
            </a>
            <a>
                <div class="box">
                    <img>
                    <p class="title">Jack</p>
                    Other content.
                </div>
            </a>
     </div>