标记:(假设列表中有许多子项,都具有相同的标记)
<ul class="notifications messages inbox">
<li class=" unread">
<div class="avatar">
<a href="index.php?userid=94"><img alt="" src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/276073_662982570_5177619_n.jpg"> </a>
</div>
<div class="txt">
<a class="userName" href="index.php?userid=94">Jose ignacio bustamante <span class="date"> - 2012-09-07 13:49:59</span></a>
<div>
<a href="messages.php?conversationid=94" class="msj"><span>Esque esto de querer cargar una conversación por defecto... como usuario no ...</span></a>
</div>
</div>
<span data-id="442" class="close">X</span>
</li>
所以我试图根据.username标记值(里面的)来过滤这个<li>
元素
/* I have used this before, its a :contains modification to handle uppercase */
$.expr[':'].icontains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
$('body').on('keyup','.filterFriends',function(){
$('.inbox li').hide().filter(':icontains("'+this.value+'")').show();
/* How to apply this icontains selector to $('.inbox li').find('.userName') ?? */
});
这有一个问题,我猜它检查整个li,我真的没有看到如何在filter()函数中选择.userName类。
有人能给我看灯吗?
答案 0 :(得分:1)
你可以试试这个:
$('body').on('keyup','.filterFriends',function(){
var li=$('.inbox li').hide();
$('.userName',li).filter(':icontains("'+this.value+'")').parents('li').show();
}
答案 1 :(得分:1)
$('body').on('keyup','.filterFriends',function(){
$('.inbox li')
.hide()
.find('.userName')
.filter(':icontains("'+this.value+'")')
.closest('.inbox li')
.show();
});