简体   繁体   English

如果没有子元素,则父元素应隐藏

[英]Parent element Should Hide if there is no Child element

I have a list item. 我有一个清单项目。 Each list contain class .filter-tag. 每个列表都包含类.filter-tag。 when I click on each .filter-tag it goes hidden. 当我单击每个.filter-tag时,它就会隐藏起来。 Here is the Example: FIDDLE 这是示例: FIDDLE

But I am wanting when all the list item is hidden, parent(.filter-tag-has-content) of this list item should also hide. 但是我想要隐藏所有列表项时,也应隐藏此列表项的parent(.filter-tag-has-content) Means if no list element is visible parent will hide also but if one if visible parent should stay visible too. 意味着如果列表元素不可见,父级也会隐藏,但如果可见父元素也应保持可见。

HTML 的HTML

<div class="filter-tag-container">
    <ul class="filter-tag-has-content">
        <li class="filter-tag" id="fl-1">
            <span class="tag-name">Filter Option 1</span>
            <span class="fa fa-times"></span>
         </li>
        <li class="filter-tag" id="fl-2">
            <span class="tag-name">Filter Option 2</span>
            <span class="fa fa-times"></span>
        </li>
        <li class="filter-tag" id="fl-3">
            <span class="tag-name">Filter Option 3</span>
            <span class="fa fa-times"></span>
        </li>
        <li class="filter-tag" id="fl-4">
            <span class="tag-name">Filter Option 4</span>
            <span class="fa fa-times"></span>
        </li>
        <li class="filter-tag" id="fl-5">
            <span class="tag-name">Filter Option 5</span>
            <span class="fa fa-times"></span>
        </li>

    </ul>
</div>

JS JS

$('.filter-tag').click(function(){
    $(this).hide();
}); 

Check the length of the visible list items as you click them. 单击它们时,请检查可见列表项目的长度。 Once there are none left, hide the list. 一旦一无所有,请隐藏列表。

$('.filter-tag').click(function(){
    $(this).hide();
    if(!$('.filter-tag:visible').length) $('ul.filter-tag-has-content').hide()
}); 

jsFiddle example jsFiddle示例

Or to use on multiple containers: 或用于多个容器:

$('.filter-tag').click(function(){
    $(this).hide();
    if(!$(this).siblings(':visible').length)$(this).parent().hide()
}); 

jsFiddle example jsFiddle示例

try this code. 试试这个代码。

$('.filter-tag').click(function(){
    $(this).hide();
    if( ! $(this).parent().children(':visible').length > 0){
        $(this).parent().hide();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM