Javascript 在jquery的每个函数上调用元素的子类

Javascript 在jquery的每个函数上调用元素的子类,javascript,jquery,each,Javascript,Jquery,Each,我在jquery上使用了一个each函数,在父函数中,它在each函数中被命名为this。我想检查我的类列表项中的类名称。但我当前的代码显示: $('.list-item').each(function(){ if($(this).text().toUpperCase().indexOf(txt.toUpperCase()) != -1){ $('.list-item').show(); } }); &

我在jquery上使用了一个each函数,在父函数中,它在each函数中被命名为this。我想检查我的类
列表项中的类
名称
。但我当前的代码显示:

         $('.list-item').each(function(){
            if($(this).text().toUpperCase().indexOf(txt.toUpperCase()) != -1){
           $('.list-item').show();
       }
    });  


<li class="list-item">
   some data <span class="name">this data</span>
</li>
$('.list item')。每个(函数(){
if($(this.text().toUpperCase().indexOf(txt.toUpperCase())!=-1){
$('.list项').show();
}
});  
  • 有些数据会破坏这些数据
  • 我应该对我的条件语句进行什么更改,以便它检查我的
    .list item
    中的类名,而不是整个
    .list item
    内容?

    可能尝试使用.find()

    试试这个

      $('.list-item').each(function(){
            if($(this).find('.name').text().toUpperCase().indexOf(txt.toUpperCase()) != -1){
                 $(this).show();
            }
      });  
    

    如果我正确阅读了您的文章,您希望使用
    $.find()
    获取
    .name
    文本。您可能还希望在循环中显示
    $(this)
    ,而不是所有
    。列表项

    $('.list item')。每个(函数(){
    var nameText=$(this.find('.name').text();
    如果(nameText.toUpperCase().indexOf(txt.toUpperCase())!=-1){
    $(this.show();
    }
    });
    
    
    
  • 有些数据会破坏这些数据
  •   $('.list-item').each(function(){
            if($(this).find('.name').text().toUpperCase().indexOf(txt.toUpperCase()) != -1){
                 $(this).show();
            }
      });