jQuery Mobile"pagebeforechange"被叫两次 [英] jQuery Mobile "pagebeforechange" being called twice

查看:148
本文介绍了jQuery Mobile"pagebeforechange"被叫两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为"pagebeforechange"设置了以下侦听器(非常类似于jQuery Mobile Documentation自己的代码),并在主页上调用了

I have the following listener set up for "pagebeforechange" (very similar to the jQuery Mobile Documentation's own code) and a link on the home page that is calling http://localhost/#product?id=255979

//Bind Listener for Product Details
$(document).bind( "pagebeforechange", function( e, data ) {
    //Only Run If Site is Initialized
    if( ajaxSite.options.initialized ) {
        if ( typeof data.toPage === "string" ) {
            var u = $.mobile.path.parseUrl( data.toPage ),
                pl = /^#product/;

            if ( u.hash.search(pl) !== -1 ) {
                console.log("showProduct being called.");
                ajaxSite.showProduct( u, data.options );
                e.preventDefault();
            }
        }
    }
});

当我打开JavaScript控制台并单击链接时,我看到以下内容:

When I open up the JavaScript console and click the link I am seeing the following:

showProduct being called.
showProduct being called.

我似乎找不到任何有关为什么它会被调用两次的信息.我还看到其他错误,其中vclicks由于边缘单击而被注册了两次,但这没有任何意义,因为它依赖于实际的页面更改.

I can't seem to find anything about why it would be getting called twice. I have seen other errors where vclicks get registered twice due to edge-clicking, but this doesn't make any sense since it is relying on the actual page change.

推荐答案

因为您要绑定到$(document)并使用多页布局

Since you're binding to the $(document) and using a Multi-page layout

我认为jQM正在多次加载文档(只是预感)

I think jQM is loading the document multiple times (Just a hunch)

切换为使用pageId,例如:

Switch to using the pageId instead, example:

$(document).bind( "pagebeforechange", function( e, data ) { ...

$('#pageId').bind( "pagebeforechange", function( e, data ) { ...

这篇关于jQuery Mobile"pagebeforechange"被叫两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆