Running jQuery code before the DOM is ready?

前端 未结 2 753
独厮守ぢ
独厮守ぢ 2020-12-06 10:24

Is there a way to execute jQuery code before the DOM is ready ?

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 10:52

    If you include jQuery in the head of your document, and then write your code in another script tag in the head, it will run before the DOM is ready:

    
        
        
    
    
        
    
    

    However, obviously you won't be able to interact with any DOM elements, since they won't exist yet.


    Having said that, there is a technique you can use in modern browsers that can allow you to interact with DOM elements as they are inserted into the page (while the browser is rendering it). It relies upon CSS animation events. I've written a simple library (Progressive.js) to make this easier to handle.

提交回复
热议问题