[英]jQuery mobile ajax load jsonp not work after page change
我使用jQuery Mobile创建了一个移动网页。 我在页面加载时使用jQuery的.ajax()
方法加载推文。 它可以工作,但是当我通过单击链接更改页面时,这些推文将不再加载。
这是HTML:
<ul data-role="listview" data-divider-theme="c" data-inset="true" id="tweets">
<li data-role="list-divider">Latest Tweets</li>
</ul>
使用Javascript:
$(document).bind('pageinit',function(){
$.ajax({
url:'https://api.twitter.com/1/statuses/user_timeline/LicsonLee.json',
dataType:'jsonp',
success:function(data){
$.each(data,function(i){
if(i < 5){
var tweet = data[i];
$('#tweets').append($('<li/>').html('<a href="https://twitter.com/'+tweet.user.screen_name+'/status/'+tweet.id_str+'" data-rel="external"><h4>'+tweet.text+'</h4><p>at '+tweet.created_at+'</p></a>'));
}
});
$('#tweets').listview('refresh');
}
});
});
目前的进展
我已经尝试过Gajotres的回答,但仍然只能使用一次。 这些页面是通过AJAX加载的。 我还检查了其他页面的HTML结构是否正确。 我仍然不知道为什么会这样。
任何帮助将不胜感激。
在这种情况下不应该使用:
$(document).bind('pageinit',function(){
它只会触发一次,而应该使用:
$(document).bind('pagebeforeshow',function(){
应该这样做:
$(document).on('pagebeforeshow', '[data-role="page"]', function(){
如果您想了解更多有关此内容的信息,请查看我的文章 ,为更加透明起见 ,这是我的个人博客。 或者可以在这里找到。
此解决方案有效:
$(document).on('pageshow',function(){
$.ajax({
url:'https://api.twitter.com/1/statuses/user_timeline/LicsonLee.json',
dataType:'jsonp',
success:function(data){
$('#tweets *:not([data-role=list-divider])').remove();
$.each(data,function(i){
if(i < 5){
var tweet = data[i];
$.mobile.activePage.find('#tweets').append($('<li/>').html('<a href="https://twitter.com/'+tweet.user.screen_name+'/status/'+tweet.id_str+'" data-rel="external"><h4>'+tweet.text+'</h4><p>at '+tweet.created_at+'</p></a>'));
}
});
$.mobile.activePage.find('#tweets').listview('refresh');
}
});
});
每次您将内容附加到第一页的#tweets
时,它只会将其附加到当前活动的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.