Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery在所有子项都隐藏时隐藏父块_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript jQuery在所有子项都隐藏时隐藏父块

Javascript jQuery在所有子项都隐藏时隐藏父块,javascript,jquery,html,css,Javascript,Jquery,Html,Css,问题是,如果所有子块都隐藏,如何隐藏父块,如果它们再次可见,如何显示父块? 是否可以使用jQuery“监视”块状态 -如果我们隐藏黄色和绿色块,红色块必须自动隐藏 Html <div id="mother"> <div class="child1"></div> <div class="child2"></div> </div> JavaScript $(function() { var childs

问题是,如果所有子块都隐藏,如何隐藏父块,如果它们再次可见,如何显示父块? 是否可以使用jQuery“监视”块状态

-如果我们隐藏黄色和绿色块,红色块必须自动隐藏

Html

<div id="mother">
    <div class="child1"></div>
   <div class="child2"></div>
</div>
JavaScript

$(function() {
    var childs = $("[class^=child]");

    childs.click(function() {
        $(this).hide();
    });
});
试一试

演示:

试试看


演示:

在子单击事件处理程序中尝试此操作:

   if($('#mother').children(':visible').length == 0) {
      $('#mother').hide();
   }

在子单击事件处理程序中尝试以下操作:

   if($('#mother').children(':visible').length == 0) {
      $('#mother').hide();
   }

您可以使用选择器和
.length
检查
#mother
div中可见元素的长度,如果它等于
0
,则隐藏
#mother

$(function () {
    var childs = $("[class^=child]");
    childs.click(function () {
        $(this).hide();
        if ($("#mother").find(":visible").length == 0) 
            $("#mother").hide();
    });
});

您可以使用选择器和
.length
检查
#mother
div中可见元素的长度,如果它等于
0
,则隐藏
#mother

$(function () {
    var childs = $("[class^=child]");
    childs.click(function () {
        $(this).hide();
        if ($("#mother").find(":visible").length == 0) 
            $("#mother").hide();
    });
});

您可以尝试:

$(function() {
    var childs = $("[class^=child]");

    childs.click(function() {
        $(this).hide();
        var $parent = $(this).parent();
        var $child = $parent.find('div:visible');

        if(!$child.length){
           $parent.hide();
        }
    });
});
你可以这样做:

$(function() {
    var childs = $("[class^=child]");

    childs.click(function() {
        $(this).hide();
        var $parent = $(this).parent();
        var $child = $parent.find('div:visible');

        if(!$child.length){
           $parent.hide();
        }
    });
});

谢谢,但问题是,如果一个或所有子元素被禁用,如何再次禁用红色块visible@Nerfair然后你需要实现一个类似的逻辑,让孩子再次可见谢谢,但问题是,如果一个或所有子元素被禁用,如何再次禁用红色块visible@Nerfair然后,您需要实现一个类似的逻辑,使子对象再次可见