JavaScript hide/show element

前端 未结 11 1812
死守一世寂寞
死守一世寂寞 2020-11-22 00:08

How could I hide the \'Edit\'-link after I press it? and also can I hide the \"lorem ipsum\" text when I press edit?



        
11条回答
  •  情书的邮戳
    2020-11-22 00:55

    I would suggest this to hide elements (as others have suggested):

    document.getElementById(id).style.display = 'none';
    

    But to make elements visible, I'd suggest this (instead of display = 'block'):

    document.getElementById(id).style.display = '';
    

    The reason is that using display = 'block' is causing additional margin/whitespace next to the element being made visible in both IE (11) and Chrome (Version 43.0.2357.130 m) on the page I'm working on.

    When you first load a page in Chrome, an element without a style attribute will appear like this in the DOM inspector:

    element.style {
    }
    

    Hiding it using the standard JavaScript makes it this, as expected:

    element.style {
      display: none;
    }
    

    Making it visible again using display = 'block' changes it to this:

    element.style {
      display: block;
    }
    

    Which is not the same as it originally was. This could very well not make any difference in the majority of cases. But in some cases, it does introduce abnormalities.

    Using display = '' does restore it to its original state in the DOM inspector, so it seems like the better approach.

提交回复
热议问题