2

jQueryMobileを使用してモバイルWebページを作成しました。ページの読み込み時に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の答えを試しましたが、それでも1回しか機能しませんでした。ページはAJAXを介してロードされます。また、他のページのHTML構造が正しいことも確認しました。なぜこれが起こったのか私はまだ理解できません。

どんな助けでもありがたいです。

4

1 に答える 1

4

解決

これは、次の場合には使用しないでください。

$(document).bind('pageinit',function(){

1 回だけトリガーされますが、代わりにこれを使用する必要があります。

$(document).bind('pagebeforeshow',function(){

編集 :

これはそれを行う必要があります:

$(document).on('pagebeforeshow', '[data-role="page"]', function(){   

これについて詳しく知りたい場合は、私のARTICLEをご覧ください。より透明性を高めるために、それは私の個人的なブログです。または、ここで見つけることができます。

編集2:

このソリューションは機能します:

$(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に、現在アクティブなページにのみ追加されます。

于 2013-02-01T12:36:16.107 に答える