在 AJAX 加载的内容中初始化 jqtouch 中的链接

发布于 2024-09-03 11:55:17 字数 683 浏览 7 评论 0原文

我有一个内置于 jqtouch 的 iPhone Web 应用程序,当通过链接(“a”标签)加载内容时,下一页上的任何内容都会由 jqtouch 解析,并且所有链接都会被初始化。但是,当我通过 AJAX 调用加载内容并将其附加到元素时,该内容中的任何链接都不会由 jqtouch 初始化。因此,对这些链接的任何点击都是对新资源的全面点击,并且不由 jqtouch 处理,因此此时您已经有效地脱离了 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>

提前致谢。

I have an iphone web app built in jqtouch, when content is loaded by links ("a" tags) then any content on the next page is parsed by jqtouch and any links are initialized. However, when I load content via an AJAX call and append() it to an element then any links in that content are NOT initialized by jqtouch. Thus any clicks on those links are full-blown clicks to a new resource and are not handled by jqtouch, so at that point you've effectively broken out of jqtouch.

My AJAX code is:

#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>

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2024-09-10 11:55:17

我可以通过做两件事来实现这一点:

1)通过修改 jqtouch.js 并将 liveTap 添加到返回的方法列表(在 jqtouch.js 的末尾),将以前的私有方法“liveTap”导出为公共方法:

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);            
      });

I was able to achieve this by doing two things:

1) Exporting the previously private method "liveTap" as public by modifying jqtouch.js and adding liveTap to the list of methods returned (at the end of jqtouch.js):

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

2) Calling the liveTap handler on my wrapped container which has the links I would like to "hook up" to jqTouch:

      $('#data a').click(function(e) {
        e.preventDefault();
        jqTouch.liveTap(e);            
      });
×眷恋的温暖 2024-09-10 11:55:17

执行以下操作对我来说效果更好,并且不需要修改源代码:

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

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

Doing the following worked better for me and it didn't require modifying the source:

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

where #slide is the ID of the panel to slide to.

红颜悴 2024-09-10 11:55:17

虽然 @Cody 的解决方案可行,但您最好在原始页面中添加容器 div,然后您可以异步填充该容器并为其添加动画。例如:在main.html中添加:


...

...

然后,在附加脚本中添加:

$('selector').append('<a href='#future">link</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%。另外,如果您正在做建议,您可以分解追加方法,首先在没有“.click”的循环中进行追加,然后使用单个 jQuery 命令来添加单击侦听器。您可以/应该添加属性以加载上面提到的上下文。

@emtrane:问题是关于 ajax 加载 jQuery 块,该块本身在链接中加载附加内容。

While @Cody's solution would work, you are better of adding container div in the originating page that you could then populate asynchronously and animate to. For example: in main.html add:


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

Then, in the appending script, add:

$('selector').append('<a href='#future">link</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');
}

Then problem with appending a link from script that then loads jQTouch containers is that you then have to expose hidden methods and even then it's not 100%. Also, if you are doing suggestions, you can break down the append method to first do appends in a loop without the '.click', then a single jQuery command to add the click listener. You can/should add attributes to the in order to load context referred to above.

@emtrane: the questions is about ajax loading a jQuery block that itself loads additional content in a link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文