隐藏/显示包含单词的ul li项目.但只能入住李的子女

Hide/show ul li items that contain a word. But check only in a children of the li

本文关键字:项目 显示 包含单 li ul 隐藏      更新时间:2023-09-26

标记:(假设列表中有许多子级,都有相同的标记)

<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类。。

有人能给我看看灯吗?

你可以试试这个:

$('body').on('keyup','.filterFriends',function(){
    var li=$('.inbox li').hide();
    $('.userName',li).filter(':icontains("'+this.value+'")').parents('li').show();
}
$('body').on('keyup','.filterFriends',function(){
    $('.inbox li')
        .hide()
        .find('.userName')
        .filter(':icontains("'+this.value+'")')
        .closest('.inbox li')
        .show();
});