在 jQuery 上调用元素的后代类每个函数



我在我的jquery上使用了一个每个函数,在我的父函数中,在我的每个函数中被命名为这个。我想检查我的班级list-item中的类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中的类名而不是整个.list-item内容?

如果我正确阅读了您的帖子,您希望使用$.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>

试试这个

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

也许尝试使用 .find()

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

最新更新