2010-06-01 78 views
0

當jqtouch中構建一個iphone web應用程序時,通過鏈接加載內容(「a」標籤),然後jqtouch解析下一頁上的任何內容並初始化任何鏈接。但是,當我通過AJAX調用加載內容並將它加載到元素時,那麼該內容中的任何鏈接都不會被jqtouch初始化。因此,對這些鏈接的任何點擊都是對新資源的全面點擊,並且不會由jqtouch處理,所以此時您已經有效地打破了jqtouch。在AJAX加載內容中初始化jqtouch中的鏈接

我的AJAX代碼是:提前

#data 
<script type="text/javascript"> 
     $.ajax({ 
     url: '/mobile/nearby-accounts', 
     type: 'GET', 
     dataType: 'html', 
     data: {lat: lat, lng: lng}, 
     success: function(html) { 
      $('#data').empty().append(html); 
      // Is there some method I call on jqtouch to have any links in $('#data') be hooked up to the jqtouch lifecycle? 
     } 
</script> 

感謝。

回答

0

我能夠做兩件事情來實現這一目標:

1)通過修改jqtouch.js和添加liveTap到方法列表導出先前私有方法「liveTap」公共返回(在結束jqtouch.js):

publicObj {getOrientation:getOrientation,goBack:goBack,goTo:goTo, 
addAnimation:addAnimation,submitForm:submitForm, liveTap:liveTap}; 
return publicObj; 

2)調用我的包裝的容器的liveTap處理器擁有我想 「掛鉤」,以jqTouch鏈接:

 $('#data a').click(function(e) { 
     e.preventDefault(); 
     jqTouch.liveTap(e);    
     }); 
0

做的FOL降脂工作更好地爲我,它並不需要修改源:

jQT.goTo('#slide', 'slide'); 

其中#slide是面板滑動到的ID。

0

雖然@ Cody的解決方案可以工作,但您最好在原始頁面中添加容器div,然後您可以異步填充並生成動畫。例如:在main.html中添加:

... <div id="future"></div> ...

然後,在追加劇本,加:

 

$('selector').append('<a href='#future"&gtlink</a>').find('a').click(function(e) { 
    e.preventDefault(); 
    loadDataAndShow($(this).<read some context here>); 
}); 

function loadDataAndShow() { 
    // Use ajax like $.getJSON or whatever to load data here 

    // populate $('#future') with data then 

    jQT.goTo('#future', 'slide'); 
} 

然後從腳本附加鏈接的問題,然後加載jQTouch容器是你那麼不得不暴露隱藏的方法,即使這樣也不是100%。此外,如果您正在提出建議,可以分解append方法以首先在沒有「.click」的循環中追加,然後使用單個jQuery命令添加點擊偵聽器。您可以/應該添加屬性以便加載上面提到的上下文。

@emtrane:問題是關於加載一個jQuery塊本身加載鏈接中的額外內容的問題。

+0

免責聲明:我正在使用舊版本的jQTouch ...所以可能有新的API來做到這一點。剛注意到這是一個老問題。 – Ephraim 2011-03-24 02:22:32