我想在<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不起作用。那么如何按标题过滤?
答案 0 :(得分:5)
要实现此目的,您可以使用filter()
查找不符合搜索要求的.title
元素,然后隐藏其父.box
。但请注意,对于不区分大小写的搜索,您需要使用自己的逻辑扩展:
jQuery运算符,该逻辑是:contains
的变体,如下所示:
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;
答案 1 :(得分:1)
#content
定位ID为content
的元素。您已将content
分配给div
,其目标应为.content
。
$('#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;
答案 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>