我使用 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
在填充后转换到页面。
我是否为我想要的效果触发了错误事件的内容加载,如果是,我应该使用哪个事件?
编辑于 2011 年 9 月 30 日
我已经设法让页面转换等到列表完全加载后再转换,但是加载消息仍然没有出现。
我用了:
$('#news').live('pagebeforeshow',function(event){
$.mobile.showPageLoadingMsg();
而不是准备好文件和.click
我用过:
$.ajax({
url: 'http://wiiuandmii.com' + url + 'feed/',
async: false,
success:function(data){
而不是.get()