[英]calling the descendant class of an element on jquery each function
I'm using an each function on my jquery where in my parent is named as this inside my each function. 我在我的jquery上使用了each函数,其中在我的parent中的每个函数中都以此命名。 I want to check the class name
inside my class list-item
. 我想检查班级list-item
的班级name
。 But my current code is showing 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>
what should I change on my conditional statement for it to check the class name inside my .list-item
instead of the entire .list-item
content? 我应该更改条件语句以检查.list-item
的类名,而不是整个.list-item
内容吗?
Try this 尝试这个
$('.list-item').each(function(){
if($(this).find('.name').text().toUpperCase().indexOf(txt.toUpperCase()) != -1){
$(this).show();
}
});
If I'm reading your post correctly, you want to use $.find()
to get the .name
text. 如果我正确地阅读了您的帖子,则想使用$.find()
获取.name
文本。 And you probably also want to show $(this)
inside of the loop, instead of all .list-item
's. 您可能还希望在循环内显示$(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>
Maybe try to to use .find() 也许尝试使用.find()
$('.list-item').each(function(){
if($(this).find('.name').text().toUpperCase().indexOf(txt.toUpperCase()) != -1){
$('.list-item').show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.