gpt4 book ai didi

javascript - jQuery - 如果所有子级都受到影响,则隐藏父级

转载 作者:行者123 更新时间:2023-11-28 15:29:29 24 4
gpt4 key购买 nike

我正在输入上使用.keyup()为多个ul创建搜索函数。如果有匹配,则 .show()li,对于其他匹配,则为 .hide()

jQuery('.brandsearch').keyup(function(event) {
var inputValue = jQuery(".brandsearch").val().toLowerCase();
jQuery(".brands-group li").each(function(event) {
if(jQuery(this).html().toLowerCase().indexOf(inputValue) == -1) {
jQuery(this).hide();
} else {
jQuery(this).show();
}
});
});

如果隐藏了其中的所有 li,如何爬上层次结构以隐藏整个 ul

HTML 布局:

<input type="text" class="brandsearch">

<ul class="brands-group">
<li>Adidas</li>
<li>Nike</li>
</ul>

最佳答案

您可以添加以下内容:

if (jQuery('.brands-group li:visible').length == 0) {
jQuery('.brands-group ul').hide();
}

关于javascript - jQuery - 如果所有子级都受到影响,则隐藏父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27904273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com