jQuery Mobile:加载消息未出现
我使用 jQuery Mobile、jQuery 和 html 制作了一个网络应用程序。它基本上是其网站的 RSS 阅读器。
$(document).ready(function() {
$('[data-role=button]').click(function() {
var $id = $(this).attr('id');
switch($id)
{
case 'news-button':
var type = 'news';
var url = '/tag/news/';
break;
//etc...
}
$.get('http://wiiuandmii.com' + url + 'feed/', function(data) {
$('#' + type + '-content').empty();
var i = 0;
$(data).find('item').each(function() {
i = i + 1;
$('#' + type + i).empty().remove();
var $item = $(this);
var html = '<li>';
html += '<a href="#'+ type + i + '">';
html += $item.find('image').text();
html += '<h3 style="color: #01acca;">' + $item.find('title').text() + '</h3>';
html += '<p>' + $item.find('pubDate').text() + '</p>';
html += '</a>';
html += '</li>';
$('#' + type + '-content').append($(html));
$('#' + type + '-content').find('img.attachment-thumbnail').removeClass().removeAttr('width').removeAttr('height');
$('#' + type + '-content').listview('refresh');
var page = '<div data-role="page" id="' + type + i + '" data-url="' + type + i + '">';
page += '<div data-role="header" data-theme="c" data-position="fixed"><a href="#home" data-rel="back" data-theme="b" data-icon="arrow-l">Back</a><h1 style="margin-top:0px; margin-bottom:0px;"><img src="images/header.png" title="Wii U and Mii" alt="Wii U and Mii"/></h1></div>';
page += '<div data-role="content" data-theme="c" class="main-' + type + i + '">';
page += '<h1 style="color: #01acca;">' + $item.find('title').text() + '</h1>';
page += '<p>' + $item.find('dc:creator').text() + '</p>';
page += $item.find('desc').text();
page += '</div>';
page += '<div data-role="footer" data-theme="c"></div>';
page += '</div>';
$('body').append($(page));
var test = '#' + type + i;
$(test).page();
});
});
});
});
单击主页上的按钮后,页面会正常转换到#news
页面。问题是,在加载内容时,#news
页面的内容部分保持空白约 5 秒。不出现加载消息。应该发生的情况是,在加载所有内容时,加载消息应该出现在 #home
页面上,然后在填充后转换到 #news
页面。
我是否在错误的事件上触发内容加载以获得我想要的效果,如果是这样,我应该使用哪个事件?
编辑于 30/9/2011
我已经设法让页面转换等待列表完全加载后再转换,但是加载消息仍然没有出现。
我使用:
$('#news').live('pagebeforeshow',function(event){
$.mobile.showPageLoadingMsg();
代替文档就绪和 .click
我使用:
$.ajax({
url: 'http://wiiuandmii.com' + url + 'feed/',
async: false,
success:function(data){
代替 .get()
I have made a web app using jQuery Mobile, jQuery and html. It is basically an rss reader for its site.
$(document).ready(function() {
$('[data-role=button]').click(function() {
var $id = $(this).attr('id');
switch($id)
{
case 'news-button':
var type = 'news';
var url = '/tag/news/';
break;
//etc...
}
$.get('http://wiiuandmii.com' + url + 'feed/', function(data) {
$('#' + type + '-content').empty();
var i = 0;
$(data).find('item').each(function() {
i = i + 1;
$('#' + type + i).empty().remove();
var $item = $(this);
var html = '<li>';
html += '<a href="#'+ type + i + '">';
html += $item.find('image').text();
html += '<h3 style="color: #01acca;">' + $item.find('title').text() + '</h3>';
html += '<p>' + $item.find('pubDate').text() + '</p>';
html += '</a>';
html += '</li>';
$('#' + type + '-content').append($(html));
$('#' + type + '-content').find('img.attachment-thumbnail').removeClass().removeAttr('width').removeAttr('height');
$('#' + type + '-content').listview('refresh');
var page = '<div data-role="page" id="' + type + i + '" data-url="' + type + i + '">';
page += '<div data-role="header" data-theme="c" data-position="fixed"><a href="#home" data-rel="back" data-theme="b" data-icon="arrow-l">Back</a><h1 style="margin-top:0px; margin-bottom:0px;"><img src="images/header.png" title="Wii U and Mii" alt="Wii U and Mii"/></h1></div>';
page += '<div data-role="content" data-theme="c" class="main-' + type + i + '">';
page += '<h1 style="color: #01acca;">' + $item.find('title').text() + '</h1>';
page += '<p>' + $item.find('dc:creator').text() + '</p>';
page += $item.find('desc').text();
page += '</div>';
page += '<div data-role="footer" data-theme="c"></div>';
page += '</div>';
$('body').append($(page));
var test = '#' + type + i;
$(test).page();
});
});
});
});
When the button on the home page is clicked it page transitions to the #news
page just fine. The problem is, the content section of the #news
page remains blank for about 5 seconds as the content is loading. The loading message does not appear. What should happen is the loading message should appear on the #home
page whilst all the content is loaded up, and then transition to the #news
page after it has been filled.
Am I triggering the loading of the content on the wrong event for the effect I want, if so which event should I be using?
EDIT on 30/9/2011
I have managed to get the page transition to wait until the list has been fully loaded before it transitions, however the loading message still does not appear.
I used:
$('#news').live('pagebeforeshow',function(event){
$.mobile.showPageLoadingMsg();
instead of document ready and the .click
and I used:
$.ajax({
url: 'http://wiiuandmii.com' + url + 'feed/',
async: false,
success:function(data){
instead of the .get()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管已经有一个可接受的答案,但我想补充一点,我也遇到了同样的问题,并且我解决了将
showPageLoadingMsg
调用从pagebeforeshow
移动到pageshow< /code> 处理程序。希望这有帮助
Although there is already an accepted answer, I wanted to add that I had the very same issue and I solved moving the
showPageLoadingMsg
call from thepagebeforeshow
to thepageshow
handler. Hope this helpsgithub-jQM 论坛上的一位成员建议了以下对我有用的解决方法。注意,我使用的是 jQM 1.0b1。
$('.ui-loader h1').text('我的自定义加载消息..');
参考:https://github.com/jquery/jquery-mobile/issues/1178
A member on the github-jQM forum suggested the following workaround that worked for me. Note, I am using jQM 1.0b1.
$('.ui-loader h1').text('my custom loading msg..');
Ref.: https://github.com/jquery/jquery-mobile/issues/1178