How to check element's visibility via javascript?

后端 未结 5 1428
后悔当初
后悔当初 2020-12-05 11:14

I\'m interested in a way to check whether an element has display:none style explicility (ie style=\"display:none\"), has a class that has (or inherits) this style, or one of

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 11:44

    Each case will need its own check and you need to know the ID of that element. First, grab the element (just doing this to make the code readable):

    var MyElementName = document.getElementById("MyElementName");
    

    Then do your checks:

    Case 1:

     if (MyElementName.style.display == "none")
    

    Case 2, looking at the parent, checking FF first:

    if ((MyElementName.previousSibling.nodeType == 3 )
        && (MyElementName.parentNode.nextSibling.style.display == "none"))
    

    then for other browsers:

    else (MyElementName.parentNode.style.display == "none")
    

    Case 3, look for an applied css class:

    if (MyElementName.className = "SomeClass")
    

提交回复
热议问题