Change element display none back to default style value JS

前端 未结 6 1004
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 11:22

I have a function in JS that hides the element parsed:

function hide(id){
  document.getElementById(id).style.display = \"none\";
}

How can

6条回答
  •  不知归路
    2020-12-20 11:51

    Here is one more solution for retrieving any property default value of any element. Idea is following:

    1. Get nodeName of specific element
    2. Append a fake element of the same node name to body
    3. Get any property value of the fake element.
    4. Remove fake element.

    function getDefaultValue(element, property) {
        var elDefault = document.createElement(element.nodeName);
        document.body.appendChild(elDefault);
        propertyValue = window.getComputedStyle(elDefault,null).getPropertyValue(property);
        document.body.removeChild(elDefault);
      return propertyValue;
    }
     function resetPropertyValue (element,propertyName) {
        var propertyDefaultValue = getDefaultValue(element, propertyName);
        if (element.style.setProperty) {
            element.style.setProperty (propertyName, propertyDefaultValue, null);
        } 
        else {
            element.style.setAttribute (propertyName, propertyDefaultValue);
        }
    }
    #d {
      background: teal;
      display: inline;
    }
    
    
    test

提交回复
热议问题