2010-06-01 35 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