簡體   English   中英

在加載jQuery之前將腳本附加到jQuery ready方法

[英]Attach script to jQuery ready method before jQuery is loaded

例如,引導程序將jQuery放在html的末尾,例如http://twitter.github.com/bootstrap/examples/starter-template.html

如果要在加載jQuery腳本本身之前插入代碼塊該怎么辦?

<div id="test1"></div> 
<div id="test2"></div> 

<script>
   $(document).ready(function() {
     $('#test1').html('test1'); // not work, any workaround? the code must be put before..
   });
</script> 

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
   $(document).ready(function() {
     $('#test2').html('test2'); // work
   });
</script>   

測試: http//jsfiddle.net/e5HKZ/

我希望同時顯示test1和test2。

任何想法?

如果確實需要將jQuery代碼放在加載jQuery本身的行之前,請將函數分配給document.ready

window.document.ready = function() {
    $('#test1').html('test1');
};

加載jQuery后,它將讀取變量並在DOM准備就緒時執行該函數。

請注意,這很可能是未記錄的行為,可能無法可靠運行或在未來的jQuery版本中。

演示: http : //jsfiddle.net/e5HKZ/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM