jQuery Mobile“pagebeforechange”被调用两次

2024-02-15

我为“pagebeforechange”设置了以下侦听器(与 jQuery Mobile 文档自己的代码非常相似),并且在主页上有一个正在调用的链接http://localhost/#product?id=255979 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 由于边缘单击而注册两次,但这没有任何意义,因为它依赖于实际的页面更改。


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

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

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

改用 pageId,例如:

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

to

$('#pageId').bind( "pagebeforechange", function( e, data ) { ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery Mobile“pagebeforechange”被调用两次 的相关文章

随机推荐