2011-06-05 99 views
0

我正在使用以下代码以jQuery动态加载页面。页面加载工作正常,但是当您单击导航链接加载新页面时,旧页面会在屏幕上闪烁,就像它再次加载前一页面一样。点击“联系”导航链接,看看我的意思:http://ghostpool.com/wordpress/buzz。这是为什么发生?jQuery页面加载器问题

jQuery的(文件)。就绪(函数(){

var hash = window.location.hash.substr(1); 
var href = jQuery('#nav li a').each(function(){ 
    var href = jQuery(this).attr('href'); 
    if(hash==href.substr(0,href.length-5)){ 
     var toLoad = hash+'.html #content'; 
     jQuery('#content').load(toLoad) 
    }           
}); 

jQuery('#nav li a').click(function(){ 

    var toLoad = jQuery(this).attr('href')+' #content'; 
    jQuery('#content').hide('fast',loadContent); 
    jQuery('#load').remove(); 
    jQuery('#page-wrapper').append('<span id="load">LOADING...</span>'); 
    jQuery('#load').fadeIn('normal'); 
    window.location.hash = jQuery(this).attr('href').substr(0,jQuery(this).attr('href').length-5); 
    function loadContent() { 
     jQuery('#content').load(toLoad,'',showNewContent()) 
    } 
    function showNewContent() { 
     jQuery('#content').show('normal',hideLoader()); 
    } 
    function hideLoader() { 
     jQuery('#load').fadeOut('normal'); 
    } 
    return false; 

}); 

});

回答

0

默认行为仍在运行。你必须禁用它使用这些方法之一:

jQuery('#nav li a').click(function(e){ 

e.preventDefault(); 

// or 

return false;// at the end of the function 
} 

编辑:

后重新阅读你的代码,我在这里看到不寻常的东西机智的功能:

function loadContent() { 
    jQuery('#content').load(toLoad,'',showNewContent()) 
} 
function showNewContent() { 
    jQuery('#content').show('normal',hideLoader()); 
} 

,而不是传递你正在传递返回值的回调函数。这里是应该的

jQuery('#content').load(toLoad,'',showNewContent); 
... 
jQuery('#content').show('normal',hideLoader); 

没有括号被删除

+0

我都试过解决方案,但没有正常工作。我想我没有正确地将它们插入到代码中。你告诉我这些代码可以在我上面发布的代码中应用这些更改的代码是什么?我应该指出,在函数结束时我已经返回false。 – GhostPool 2011-06-05 09:21:55

+0

@GhostPool,我没有注意到最后的返回错误。无论如何,我发现一个错误看到我的编辑。 – Ibu 2011-06-05 09:29:43

+0

解决了这个问题。谢谢! – GhostPool 2011-06-05 09:34:11