使用jquery.load()加载div
我创建了一个网页,并使用load()函数在fixed div上加载这些网页。但是,如果连续点击不同的链接,整个网页就会停止响应 以下是我加载页面的代码:使用jquery.load()加载div,jquery,load,Jquery,Load,我创建了一个网页,并使用load()函数在fixed div上加载这些网页。但是,如果连续点击不同的链接,整个网页就会停止响应 以下是我加载页面的代码: $( document ).ready( function() { $('a').bind('click' , function( e ) { e.preventDefault(); // prevent the browser from following the link
$( document ).ready( function() {
$('a').bind('click' , function( e ) {
e.preventDefault(); // prevent the browser from following the link
e.stopPropagation(); // prevent the browser from following the link
$( '#part1' ).load( $( this ).attr( 'href' ) );
});
});
我还想在单击上面load()函数的链接时更改url。通过连续单击,您正在添加一组在后面运行的关联请求。它们都需要处理。因此,浏览器将其内容添加到
#part1
,并在每次请求返回时呈现它。这可能是页面没有响应的原因
编辑
您可以显示一个指示器,让用户知道他正在做什么,并防止再次执行加载。以下示例显示了在加载零件时防止执行加载
$( document ).ready( function() {
var isLoading = false;
$('a').bind('click' , function( e ) {
e.preventDefault(); // prevent the browser from following the link
e.stopPropagation(); // prevent the browser from following the link
if(!isLoading){
//start loading
isLoading = true;
$( '#part1' ).load( $( this ).attr( 'href' ), function(response, status, xhr) {
//this is the callback method for success or failure
//stop loading
isLoading = false;
});
}
});
});
指示器的显示/隐藏应该不难添加。开始加载时显示,停止加载时隐藏。您可能没有复制完整的代码,或者缺少最终关闭代码。关闭“ready函数”(当前代码仅关闭“click函数”)。是否有其他方法可替代此方法。.load()load是一种简写。你可以用普通的。但就你的情况而言,我想这并不是一个糟糕的选择。。你为什么还要别的?它不是像你想的那样工作吗?我有一个页面…我想所有的内容都应该加载,而不是刷新…但是随着点击次数的增加…它停止响应,需要花费很多时间来响应…还有其他一些事情我想更改url,让用户感觉它正在导航..就像在google+中,如果它变慢了,你可能会保留一些东西你不应该留下的。类似于重复或过时的事件处理程序/重画逻辑或其他混乱的代码。如果没有更多的代码,很难判断。。。url部分是