JQueryMobile 1.4 有 deprecated pageshow 事件,而是建议使用 pagecontainershow;但是,虽然我能够在文档级别获取 pagecontainershow 事件,但我无法将函数绑定(bind)到特定页面。
<div id="page1" data-role="page">
...
<script>
$( "#page1" ).on( "pagecontainershow", function( event, ui ) {
console.log("page1 pagecontainershow");
} );
</script>
</div>
演示:http://jsbin.com/IFolanOW/22/edit?html,console,output
我还考虑在我们使用选择器的情况下使用 jQuery“on”函数的替代形式,但这需要是页面 div 的父级,并且可能包括其他页面,所以这不起作用。
作为一种解决方法,我已经这样做了,但是效率非常低:
function registerOnPageShow(pageId, func) {
var strippedPageId = pageId.replace("#", "");
var e = "pagecontainershow." + strippedPageId;
// TODO why isn't it working to use $(pageId) instead of $(document)?
$( document ).off(e).on(e, null, {page: strippedPageId, f: func}, function(e, ui) {
if ($(":mobile-pagecontainer").pagecontainer("getActivePage")[0].id == e.data.page) {
e.data.f(e, ui);
}
});
}
最佳答案
您可以像这样获取页面ID。
$(document).on('pagecontainershow', function(e, ui) {
var pageId = $('body').pagecontainer('getActivePage').prop('id');
});
目前无法在特定页面上显示/隐藏事件。
关于jquery-mobile - jQueryMobile : pagecontainershow on a particular page not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828948/