Ajax 中的加载指示器
我知道这个问题在这个论坛上已经被问过很多次了,也被回答过很多次了。但它不适用于我正在寻找的东西。
我想在 ajax div 加载时显示加载指示器。在某些情况下,加载需要几分钟,因此最好让用户知道正在加载。
据我所知,它可以在 jquery 中完成。我对 jquery 还不太熟悉。使用此代码可以加载,但仅适用于第一页。
$(document).ready(function() {
$('body').append('<div id="ajaxBusy"><p><img src="ajax-loader.gif"></p></div>');
});
$(document).ajaxStart(function(){
$('#ajaxBusy').show();
}).ajaxStop(function(){
$('#ajaxBusy').hide();
});
我的页面结构如下
标题页
-Div(此处显示ajax)
<块引用>-第一个加载页面中的另一个div(此处通过ajax加载另一个页面)
我需要它在加载时在第二个div中显示加载指示器。我假设 jquery“body”将其附加到主页正文一次,并且不会再次运行,因为它位于同一页面内。我确实尝试创建一个 div,而不是 body,而是在 jquery 中加载 div,但它不起作用。
任何帮助将不胜感激。
I know this has been asked and answered many times in this forum. But it does not work in what I am looking for.
I want to display a loading indicator while the ajax div is loading. There are cases when it takes a few minutes to load so it would be good to let the user know that the is loading.
From what I gathered it can be done in jquery. I am not too familiar with jquery yet. With this code the loading works but only for the first page.
$(document).ready(function() {
$('body').append('<div id="ajaxBusy"><p><img src="ajax-loader.gif"></p></div>');
});
$(document).ajaxStart(function(){
$('#ajaxBusy').show();
}).ajaxStop(function(){
$('#ajaxBusy').hide();
});
My page is structured like this
Header Page
-Div (display ajax here)
-Another div within the first loaded page(Load another page through ajax here)
I need it to display the loading indicator in the second div while it's loading. I am assuming that jquery "body" appends it to the main page body once and doesn't run again as it's within the same page. I did try to create a div and instead of body, load the div in jquery but it doesn't work.
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现将加载器 gif 添加到特定元素的最简单方法是创建一个以加载器作为背景的 CSS 类,而不是附加实际图像:
然后,您只需将该类添加到正在加载的元素中,并在加载时将其删除。已完成:
I found that the easiest way to add the loader gif to specific elements is to create a CSS class with the loader as a background instead of appending an actual image:
Then you just add that class to the element you are loading and remove it when it is done:
考虑到您要加载图像的 div 有一个 id="theDiv"
considering that the div you want to load your image has an id="theDiv"
您通过 Javascript 附加“ajaxBusy”div 是否有原因?为什么不直接将 HTML 包含在页面本身中呢?
尝试将 ajaxStart 和 ajaxStop 绑定到 ajaxBusy div 而不是文档。
Is there a reason you're appending your "ajaxBusy" div via Javascript? Why not just include the HTML on the page itself?
Try binding the ajaxStart and ajaxStop to the ajaxBusy div instead of the document.