我使用load()函数通过单击菜单中的按钮从不同页面动态获取细节。其中一个按钮指向使用自己的jQuery效果的图像库。但是这些脚本没有运行。我试图使用getscript()显式加载它们,但那也不起作用。这里的代码:jquery:在ajax加载后运行脚本()
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('#nav li a').each(function(){
var href = $(this).attr('href');
if((hash==href.substr(0,href.length-4))&&(hash!='')){
var toLoad = hash+'.php #content';
$('#content').load(toLoad);
}
});
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide();
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
$('#content').load(toLoad,showNewContent());
function showNewContent() {
$('#content').fadeIn(1000);
$.getScript("gallery.js");
}
return false;
});
});
这是我的gallery.js文件的内容。确切的效果可能是无关紧要的,我已经粘贴下面的代码,以显示我的警报盒的位置:
$(document).ready(function(){
//perform actions when DOM is ready
var z = 0; //for setting the initial z-index's
var inAnimation = false; //flag for testing if we are in a animation
alert("1");
$('#pictures img').each(function() { //set the initial z-index's
z++; //at the end we have the highest z-index value stored in the z variable
$(this).css('z-index', z); //apply increased z-index to <img>
alert("2");
});
function swapFirstLast(isFirst) {
if(inAnimation) return false; //if already swapping pictures just return
else inAnimation = true; //set the flag that we process a image
var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
alert("3");
if(isFirst) { processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
else { processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1; } //set variables for "previous" action
$('#pictures img').each(function() { //process each image
alert("4");
if($(this).css('z-index') == processZindex) { //if its the image we need to process
$(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
$(this).css('z-index', newZindex) //set new z-index
.animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
inAnimation = false; //reset the flag
});
});
} else { //not the image we need to process, only in/de-crease z-index
$(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
$(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
});
}
});
return false; //don't follow the clicked link
}
$('#next1 a').click(function() {
alert("5");
return swapFirstLast(true); //swap first image to last position
});
$('#prev1 a').click(function() {
alert("6");
return swapFirstLast(false); //swap last image to first position
});
});
//Function for z-index based gallery ends here
这里是我的发现:
我做了CTRL + F5并加载从开始页面,然后点击图库按钮。我得到警报“1”。图像加载,但动画不起作用。
我再次单击画廊按钮,无需刷新,我从1到6获得所有适当的警报,效果就像一个魅力。
当效果不起作用时,我只需将gallery.js的内容放在chrome控制台中,然后单击enter,并且效果再次完美。
此外,有些时候效果不起作用,尽管重复上述步骤是准确的。这里可能是什么问题?我听说getscript方法是异步的,我甚至尝试设置ajax同步关闭,但我仍然得到不可预知的结果。
使用成功:funcion()回调调用脚本的东西加载后 –