在 jquery 每个函数上调用元素的后代 class

calling the descendant class of an element on jquery each function

我在我的 jquery 上使用了一个 each 函数,在我的 each 函数中,我的父函数被命名为 this 。我想检查 class list-item 中的 class name。但是我当前的代码显示的是:

         $('.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 中的 class 名称而不是整个 .list-item 内容?

也许尝试使用 .find()

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

试试这个

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

如果我没看错你的 post,你想使用 $.find() 来获取 .name 文本。您可能还想在循环内显示 $(this),而不是所有 .list-item

$('.list-item').each(function() {
  var nameText = $(this).find('.name').text();
  if (nameText.toUpperCase().indexOf(txt.toUpperCase()) != -1) {
    $(this).show();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="list-item">
   some data <span class="name">this data</span>
</li>