有没有办法使用jquery(或其他方法)在页面加载时显示加载div?
我有一个使用 PHP/MySQL 填充的表,可以包含数千行。然后使用 jquery 的 tablesorter 插件对其进行排序。一切工作正常,但页面有时可能需要 4-5 秒才能完全加载,最好在 div 中显示“正在加载...”消息,该消息在加载整个表格时会自动消失。
我听说过 jquery 的 loadmask 插件 - 这是否适合我的需求,如果没有其他选择?
如果相关的话,加载此表时不会进行任何 AJAX 调用。
提前致谢
安迪
Is there a way to use jquery (or other method) to display a loading div while page loads?
I have a table that is populated using PHP/MySQL and can contain several thousand rows. This is then sorted using the tablesorter plugin for jquery. Everything works fine, however the page can sometimes take 4-5 seconds to fully load and it would be nice to display a 'loading...' message within a div which automatically disappears when whole table is loaded.
I have heard of loadmask plugin for jquery - would this be suitable for my needs and if not any alternative?
No AJAX calls are being made while loading this table if thats relevant.
Thanks in advance
Andy
发布评论
评论(4)
我认为实现这一点的最简单方法是使用 AJAX。您需要两种方法(脚本)。第一个方法将加载带有加载图像的初始页面和一些 JavaScript 来调用第二个方法。第二种方法将返回表的 HTML 和 javascript(在 body 元素中,而不是 head 中)来调用 tablesorter。 JavaScript 将调用第二个方法,获取 HTML,并将其插入到页面上,替换加载消息。
例子:
I think the easiest way to accomplish this would be using AJAX. You'd need two methods (scripts). The first would load the initial page with the loading image and a bit of javascript to call the second method. The second method would return the HTML for your table and the javascript (in the body element, not the head) to invoke tablesorter. The javascript would call the second method, get the HTML, and insert it onto the page, replacing the loading message.
Example:
我希望这个链接对你有一些帮助..(因为我相信你不想使用ajax)
http://forums.codecharge.com/posts.php?post_id=85584
谢谢
i hope this link can be of some help to you..(since i believe that you dont want to use ajax)
http://forums.codecharge.com/posts.php?post_id=85584
thanks
不知道这是否是您想要的,但您可以只显示一个“正在加载”以及何时表排序插件准备就绪。显示表格并隐藏加载 div。这种方法有一些缺点和优点。
优点:
1.真正易于实施。
2. 加载div将显示,直到表格排序完成。不知道表排序,但由于它是客户端代码,因此很可能会出现阻塞。
缺点:
1. 如果加载时间长是由于服务器需要很长时间才能响应请求,这对您没有帮助。那么您最好使用 AJAX 解决方案。请参阅 tvanfosson 的回复。
..弗雷德里克
Don't know if this is what you are after but you could just display a that says "loading" and when the tablesort plugin in is ready. Show the table and hide the loading div. This method has some cons and pros.
Pros:
1. Real easy to implement.
2. The loading div will show until the tablesort is done. Don't know about the tablesort but since it's client code it's a big possibility that the hold up is here.
Cons:
1. If the long loading time is due to the it take the server long to respond to the request this will not help you. Then you are better of with an AJAX solution. See tvanfosson's response.
..fredrik
我会尝试以下操作:
作为您身体中的第一个子元素,插入 div 元素,例如:
对其进行适当的样式设置,例如:
然后使用以下 jQuery:
我现在无法测试它,我希望它没有太多错误...
I would try the following:
As the first child in your body, insert the div element like:
Style it appropriately like e.g.:
Then use the following jQuery:
I can't test this right now, I hope it doesn't have too many bugs...