如何防止jQuery Mobile在动态注入html页面时进行ajax调用

时间:2011-02-25 21:59:47

标签: jquery ajax jquery-mobile dynamic

我没有使用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

1 个答案:

答案 0 :(得分:0)

你能使用event.preventDefault();?

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

或者,如果你设置rel ="外部"和链接中的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>