How can I detect DOM ready and add a class without jQuery?

后端 未结 8 1392
独厮守ぢ
独厮守ぢ 2020-11-28 06:04

I want to rewrite this line without using jQuery so it can be applied quicker (and before the downloading of the jQuery library). The line is...

$(document).         


        
8条回答
  •  青春惊慌失措
    2020-11-28 07:00

    If your aim is to add the class to body immediately as the page is loaded, perhaps to hide no-JS-fallback elements, you could do that just immediately inside the body tag rather than waiting for any events:

    
        
    

    (although in general if that's the aim it's better to remove the fallback elements as you go along replacing them with scripted elements, so that if one piece of script errors out all the other components on the page don't break.)

    This is the fastest way to bind to elements: do so just immediately after creating them (inside the open tag if you only need to alter the elements; just after the close tag if you need to alter their contents). However this approach does tend to litter the page with ugly

提交回复
热议问题