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

后端 未结 8 1377
独厮守ぢ
独厮守ぢ 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 06:55

    If you want to reproduce the jQuery's document.ready event, you can use the onreadystatechange or DOMContentLoaded events where applicable:

    function domReady () {
      document.body.className += " javascript";
      // ...
    }
    
    // Mozilla, Opera, Webkit 
    if ( document.addEventListener ) {
      document.addEventListener( "DOMContentLoaded", function(){
        document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
        domReady();
      }, false );
    
    // If IE event model is used
    } else if ( document.attachEvent ) {
      // ensure firing before onload
      document.attachEvent("onreadystatechange", function(){
        if ( document.readyState === "complete" ) {
          document.detachEvent( "onreadystatechange", arguments.callee );
          domReady();
        }
      });
    }
    

提交回复
热议问题