我试图用 jquery 显示所有以 01 结尾的标签并隐藏以 01 结尾的标签
我尝试了以下操作,但我不确定如何使用 jquery 传递以下内容
$('.options li label:contains("' + $(this).endsWith("_01") + '")').show();
$('.options li label:contains("' + $(this).endsWith("!(_01)") + '")').hide();
这是我正在使用的示例。
<ul class="options">
<li><label>ENGLISH_03</label></li>
<li><label>ENGLISH_02</label></li>
<li><label>JAPANES_01</label></li>
<li><label>CHINESE_05</label></li>
</ul>
最佳答案
您需要使用.filter()
Reduce the set of matched elements to those that match the selector or pass the function's test.
$('.options li label').filter(function(){
return $(this).text().endsWith("_01")
}).show();
$('.options li label').filter(function(){
return $(this).text().endsWith("_01")
}).parent().hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul class="options">
<li><label>ENGLISH_03</label></li>
<li><label>ENGLISH_02</label></li>
<li><label>JAPANES_01</label></li>
<li><label>CHINESE_05</label></li>
</ul>
关于javascript - 隐藏列表中的标签,具体取决于它是否以 jquery 的子字符串结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263429/