Check, using jQuery, if an element is 'display:none' or block on click

后端 未结 7 1412
我在风中等你
我在风中等你 2020-11-28 17:56

I want to check and sort elements that are hidden. Is it possible to find all elements with attribute display and value none?

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 18:30

    You can use :visible for visible elements and :hidden to find out hidden elements. This hidden elements have display attribute set to none.

    hiddenElements = $(':hidden');
    visibleElements = $(':visible');
    

    To check particular element.

    if($('#yourID:visible').length == 0)
    {
    
    }
    

    Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero, Reference

    You can also use is() with :visible

    if(!$('#yourID').is(':visible'))
    {
    
    }
    

    If you want to check value of display then you can use css()

    if($('#yourID').css('display') == 'none')
    {
    
    }
    

    If you are using display the following values display can have.

    display: none

    display: inline

    display: block

    display: list-item

    display: inline-block

    Check complete list of possible display values here.

    To check the display property with JavaScript

    var isVisible = document.getElementById("yourID").style.display == "block";
    var isHidden = document.getElementById("yourID").style.display == "none"; 
    

提交回复
热议问题