jquery-mobile - jQuery Mobile "pagebeforechange"被调用两次

标签 jquery-mobile

我为“pagebeforechange”设置了以下监听器(非常类似于 jQuery Mobile 文档自己的代码)和主页上调用 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 控制台并单击链接时,我看到以下内容:
showProduct being called.
showProduct being called.

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

最佳答案

由于您绑定(bind)到 $(document) 并使用多页布局

  • http://jquerymobile.com/demos/1.0/docs/pages/index.html

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

    改用 pageId,例如:
    $(document).bind( "pagebeforechange", function( e, data ) { ...
    


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

    关于jquery-mobile - jQuery Mobile "pagebeforechange"被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761859/

    相关文章:

    html - JQuery Mobile 添加主屏幕选项?

    javascript - 为什么本次测试中的 Qunit 计数器始终达不到目标值?

    javascript - Jquery mobile 如何删除 ui-body 类周围的边框

    jquery - 我们可以覆盖 Jquery 移动 theroller 吗?

    jquery-mobile: silentScroll 无法从 initPage 工作

    提交后过渡期间jquery移动页面闪烁

    javascript - jQuery Mobile Theme Roller 生成损坏的主题?

    javascript - 移动设备上的电话号码链接检测

    jQuery Mobile 1.4.0 拆分按钮,右键只有CSS

    dynamic - 将动态链接添加到 jQuery Mobile