How to show / hide multiple elements at the same time with javascript

前端 未结 5 437
名媛妹妹
名媛妹妹 2021-01-22 01:11

So, I have this between my head tags



        
5条回答
  •  悲&欢浪女
    2021-01-22 01:55

    The disadvantage of showing elements on ready is that they will only flicker in after the page has finished loading. This usually looks odd.

    Here's what I usually do. In a script in the of the document (which runs before the body begins to render), do this:

    document.documentElement.className = "JS";
    

    Then, any CSS selectors that descend from .JS will only match if JavaScript is enabled. Let's say you give your links a class of javascriptNeeded (a class is more appropriate than a name here). Add this to your CSS:

    .javascriptNeeded{
     display: none;
    }
    .JS .javascriptNeeded{
    
     display: inline;
    }
    

    …and the elements will be there from the start, but only if JavaScript is enabled.

提交回复
热议问题