Jquery - How to check, if child in hidden DIV is visible?

前端 未结 6 753
南方客
南方客 2020-12-17 21:06

how can i check a div in a hidden div ... if visible or not?

HTML

相关标签:
6条回答
  • 2020-12-17 21:16

    The :visible selector does not work like this

    Elements can be considered hidden for several reasons:

    • They have a CSS display value of none.
    • They are form elements with type="hidden".
    • Their width and height are explicitly set to 0.
    • An ancestor element is hidden, so the element is not shown on the page.

    if you want to check for css properties you can create a custom css selector as displayed in Select Element By CSS style (all with given style)

    0 讨论(0)
  • 2020-12-17 21:20

    Use filters to check the display style, an example on jsFiddle

    $("div")
    .filter(function(){
        return $(this).css("display") == "none";
    }).find("> div").filter(function() {
        return $(this).css("display") != "none";
    }).length
    

    Reference

    • jQuery .filter()
    0 讨论(0)
  • 2020-12-17 21:23

    I've saved these two selector extensions which is essentially the same as Steve's version:

    From another SO answer:

    // jQuery selector to find if an element is hidden by its parent
    jQuery.expr[':'].hiddenByParent = function(a) {
     return $(a).is(':hidden') && $(a).css('display') != 'none' && $(a).css('visibility') == 'visible';
    };
    

    From Remy Sharp & Paul Irish:

    // reallyvisible - by remy sharp & paul irish
    // :visible doesn't take in to account the parent's visiblity - 'reallyvisible' does...daft name, but does the job.
    // not neccessary in 1.3.2+
    $.expr[ ":" ].reallyvisible = function(a){ return !(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length); };
    
    0 讨论(0)
  • 2020-12-17 21:26

    You could check the display property of the css:

    if ($("#two_child").css("display") != "none") {
        //...
    }
    

    As Gaby points out in the comments, this would not work if your elements are being hidden using visibility, so you may want to extend it to:

    var $child = $("#two_child");
    if ($child.css("display") != "none" && $child.css("visibility") != "hidden") {
        //...
    }
    
    0 讨论(0)
  • 2020-12-17 21:31

    Because a child of a hidden element will always be hidden from display, you might try

    $("#child_two").css("style") == "none"
    

    This only works when you hide stuff with css though.

    0 讨论(0)
  • 2020-12-17 21:36

    just return false :-) An item in a box you cant find... well... you probably lost that item with the box ;-)

    0 讨论(0)
提交回复
热议问题