0

我想对具有“可搜索”类的 div 执行搜索,但我不确定应该如何选择它。我之前尝试过类似代码的东西,但它没有用。

                $('#SEARCH').change(function () {
                //if (e.which == 13) {
                    var txt = $('#SEARCH').val();
                    $('.divin').each(function(){
                       if($(this + ' > .searchable').text().toUpperCase().indexOf(txt.toUpperCase()) == -1){
                           $(this).hide();
                       }
                    });
                //}
            });

($(this + ' > .searchable')不正确

HTML:

<li id="listItem_156" class="divin">
<div style="width:30px;">
    <a href="#?w=800" rel="popup_name" id="156" class="poplight">
        <img height="16" style="cursor:move;" width="16" src="img/arrow.png" border="0"  />
    </a>
</div>
<div style="width:220px;" class="searchable">
    <a href="action=MemberDetails&item=156" class="body">Mike Frank </a> 
</div>
<div style="width:110px;">
    0 
</div>

4

4 回答 4

2

利用jQuery.find

if($(this).find(' >.searchable').text().toUpperCase().indexOf(txt.toUpperCase()) == -1){
      $(this).hide();
}
于 2013-04-13T19:00:01.860 回答
0

为什么不直接这个

 $('.divin .searchable').each(function(){

因此

 $('#SEARCH').change(function () {
                //if (e.which == 13) {
                    var txt = $('#SEARCH').val();
                    $('.divin .searchable').each(function(){
                       if($(this).text().toUpperCase().indexOf(txt.toUpperCase()) == -1){
                           $(this).parent().hide();
                       }
                    });
                //}
            });
于 2013-04-13T19:05:46.347 回答
0

在 jsfiddle

    $('#SEARCH').change(function () {
        var txt = $('#SEARCH').val();
        $('.divin .searchable').show().filter(function () {
              return $(this).text().toUpperCase().indexOf(txt.toUpperCase()) === -1
        }).hide();

    });
于 2013-04-13T19:17:41.643 回答
0

你有两种方法:

首先是.find()

$('.divin').each(function(){
   if($(this).find('> .searchable').text().toUpperCase().indexOf(txt.toUpperCase()) == -1){
        $(this).hide();
   }
});

第二个给定的上下文是这样的:

$('.divin').each(function(){
   if('> .searchable', this).text().toUpperCase().indexOf(txt.toUpperCase()) == -1){
        $(this).hide();
   }
});
于 2013-04-13T19:10:44.660 回答