我实际上可以做了与我工作的网站类似的东西。你要做的是为主页上的$ .load()调用的每个页面创建一个回调函数。
请看下面的代码从the jquery.load() documenation:
$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
});
你的具体情况,你会希望这样的事情主要测试load.html页面上。
$(document).ready(
function(){
$('li').click(function(){
var showThisContent = this.id;
$('#content').load('test-load-'+showThisContent+'.html', function(){
if (showThisContent == "one"){
//Do logic for test-load-one.html
//Pre-load your images here.
//You may have to assign a class to your anchor tag
//and do something like:
$('a.assignedClass').mouseover(function(){});
$('a.assignedClass').mouseout(function(){});
} //end if
if (showThisContent =="two"){
//Do logic for test-load-two.html here
$('.slideshow').cycle({
fx: 'fade',
speed: 500,
timeout: 0,
next: '.nextSSimg',
prev: '.prevSSimg',
pager: '#SSnav',
cleartype: true,
cleartypeNoBg: true
}); //end .cycle()
} //end if
); //end .load(location, callback function())
}); //end $('li).click()
}); //end $(document).ready()
现在,很明显,我没有转换的所有代码,但这里发生了什么是一旦的document.ready完成后,回调函数就会运行,因为像“的元素.slideshow”现在加载进入DOM,您的回调代码将适当地绑定到它们。你可以用几种方式切换这段代码,使其具有相同的结果(即,将2个$ .load()包装进条件中,而不是在.load回调中执行条件逻辑,和/或将一个callbackOne )和callbackTwo()函数上面的document.ready,然后适当调用它们),但这是你的偏好。您应该能够使用$ .load()的回调函数参数来执行您想要的操作。
我删除了document.ready块,它仍然失败。 – jac 2010-03-04 16:12:18
我更新了答案。 – Pointy 2010-03-04 16:16:37
不好。我改变了主要文档,只是在#content-area div(不是整个HTML文件)中引入并移动了该div内的脚本。仍然失败。 – jac 2010-03-04 16:22:48