获取正在 jquery 移动设备中创建的页面
Get the page being created in jquery mobile
我正在使用 jQuery 移动版 1.4.5
我将以下事件添加到文档中:
$(document).on("pagecreate", function (e) {
});
当要查看新页面时会触发该事件,我的问题是如何获取正在创建的页面的id?
$.mobile.activePage
未定义,页面容器小部件的 getActivePage()
抛出错误:
cannot call methods on pagecontainer prior to initialization; attempted to call method 'getActivePage'
感谢任何帮助。
编辑:
我用过:
$.mobile.pageContainer.pagecontainer("getActivePage")[0].id
它不会抛出错误,但它 returns 当前页面的 id 而不是要创建的页面,有什么建议吗?
检查event.target
,这里是演示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).on('mobileinit', function () {
function showme(e, ui){
var activePage = $(":mobile-pagecontainer").pagecontainer("getActivePage").prop("id");
$("#events").append("Event: "+e.type+" raised from: "+(activePage ? activePage : 'landing')+"<br>");
if(activePage) {
$("#events").append("Active Page: "+activePage+"<br>");
}
if(e.target){
if(e.target.id)
$("#events").append("Target Page: "+e.target.id+"<br>")
}
}
$(document).on("pagecreate", function(e, ui) { showme(e, ui); });
});
</script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="page-1">
<div data-role="header"><h1>Page 1</h1>
<a href="#page-2" class="ui-btn ui-corner-all ui-btn-inline ui-mini">Go to Page 2</a>
</div>
<div role="main" class="ui-content">
<div id="events">
</div>
<hr>
Click "Go to page 2"
</div>
</div>
<div data-role="page" id="page-2">
<div data-role="header"><h1>Page 2</h1>
<a href="#page-1" class="ui-btn ui-corner-all ui-btn-inline ui-mini">Back to Page 1</a>
</div>
<div role="main" class="ui-content">
Page 2 has been created - Click back
</div>
</div>
</body>
</html>
我正在使用 jQuery 移动版 1.4.5
我将以下事件添加到文档中:
$(document).on("pagecreate", function (e) {
});
当要查看新页面时会触发该事件,我的问题是如何获取正在创建的页面的id?
$.mobile.activePage
未定义,页面容器小部件的 getActivePage()
抛出错误:
cannot call methods on pagecontainer prior to initialization; attempted to call method 'getActivePage'
感谢任何帮助。
编辑:
我用过:
$.mobile.pageContainer.pagecontainer("getActivePage")[0].id
它不会抛出错误,但它 returns 当前页面的 id 而不是要创建的页面,有什么建议吗?
检查event.target
,这里是演示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).on('mobileinit', function () {
function showme(e, ui){
var activePage = $(":mobile-pagecontainer").pagecontainer("getActivePage").prop("id");
$("#events").append("Event: "+e.type+" raised from: "+(activePage ? activePage : 'landing')+"<br>");
if(activePage) {
$("#events").append("Active Page: "+activePage+"<br>");
}
if(e.target){
if(e.target.id)
$("#events").append("Target Page: "+e.target.id+"<br>")
}
}
$(document).on("pagecreate", function(e, ui) { showme(e, ui); });
});
</script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="page-1">
<div data-role="header"><h1>Page 1</h1>
<a href="#page-2" class="ui-btn ui-corner-all ui-btn-inline ui-mini">Go to Page 2</a>
</div>
<div role="main" class="ui-content">
<div id="events">
</div>
<hr>
Click "Go to page 2"
</div>
</div>
<div data-role="page" id="page-2">
<div data-role="header"><h1>Page 2</h1>
<a href="#page-1" class="ui-btn ui-corner-all ui-btn-inline ui-mini">Back to Page 1</a>
</div>
<div role="main" class="ui-content">
Page 2 has been created - Click back
</div>
</div>
</body>
</html>