用jquery的load方法载入某个页面,如何使该页面上的$(document).ready在callback之前执行。
用jquery的load方法载入某个页面,如何使该页面上的$(document).ready在callback之前执行。
代码如下:
1.html
- HTML code
<script language="javascript" src="jquery-1.5.2.min.js"></script><script language="javascript">function callback() { alert('load success!'); }function loadcontent() { $('#content').load('2.html', callback);}</script><input type="button" value="显示结果" onclick="loadcontent()" /><br><div id="content" style="width:300px; height:200px; border: 1px solid #999"></div>2.html
- HTML code
<p>页面已经成功加载。</p><script language="javascript" src="jquery-1.5.2.min.js"></script><script language="javascript">window.onload = function(){ alert('window loaded!'); }$(document).ready(function(){ alert('document ready!');});</script>如上,如何使alert('document ready!')先于alert('load success!')执行,另外,如何才能使window.onload执行?
[解决办法]
只能用延迟,load跟你载入的页面中的函数是不同线程中执行的(好像是可以这么说吧 - - ),所以在load中无法控制你所载入的页面
$('#content').load('2.html', setTimeout(callback(),1000));