【问题标题】:How to prevent jQuery Mobile from making ajax calls when dynamically injecting html pages动态注入html页面时如何防止jQuery Mobile进行ajax调用
【发布时间】:2011-02-25 21:59:46
【问题描述】:

我没有使用 ajax 调用,而是创建页面并将其注入到 $.mobile.pageContainer 中。 Dynamically creating jQuery Mobile pages using jQuery Templates

当我想访问带有井号标签(在我的 onReady 函数中生成的标签)的页面时,jQuery mobile 会尝试进行 ajax 调用。它失败。当我的 onReady 函数被调用时,我必须检查 url 并调用 $.mobile.changePage() 使其显示出来。

var loc = window.location.href;
var loc = loc.split('#').pop();
if (loc !== "http://lift.pageforest.com/") {
    $.mobile.changePage(loc, 'pop', false, true);
}

这一切都很好,但是 jQuery Mobile 仍然进行了失败的 ajax 调用,从而导致向控制台抛出错误以及向用户显示一个大错误 div。

我尝试将 $.mobileinit 函数 ajaxEnabled() 覆盖为 false,因为我永远不会使用 ajax。 http://jquerymobile.com/demos/1.0a3/#docs/api/globalconfig.html 不幸的是,这带来了一大堆其他问题。

为什么jQuery mobile会自动假定我要使用ajax,而我自己的onReady函数不会生成任何内容?我该如何解决这个问题?

从这里转贴: http://forum.jquery.com/topic/how-to-disable-automatic-ajax-calls-when-dynamically-creating-pages

【问题讨论】:

  • 你的页面有演示吗?

标签: jquery ajax jquery-mobile dynamic


【解决方案1】:

你可以使用 event.preventDefault(); 吗?

http://api.jquery.com/event.preventDefault/

或者,如果您在链接中设置 rel="external" 和 JQ 类选择器,它将阻止默认的内部链接。

<a href="#mylink" class="hash-link" rel="external">Link</a>

<script>  
$('.hash-link').click(function() {

   var loc = window.location.href;
   var loc = loc.split('#').pop();
   if (loc !== "http://lift.pageforest.com/") {
        $.mobile.changePage(loc, 'pop', false, true);
    }

});
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2016-10-10
    • 2023-03-10
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多