我为“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) 并使用多页布局
我认为 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/