0

例如,bootstrap 将 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。

任何想法?

4

1 回答 1

0

If you really have to put jQuery code before the line that loads jQuery itself, assign the function to document.ready.

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

When jQuery has been loaded, it reads the variable and executes the function at DOM ready.

Note that this is most likely undocumented behavior and might not work reliably or in future jQuery versions.

Demo: http://jsfiddle.net/e5HKZ/1/

于 2013-02-16T16:52:39.913 回答