Chi*_*lap 3 html jquery jquery-mobile
我正在尝试将一个对象从一个页面传递到另一个页面并且符合JQM文档,这应该是正确的:
$.mobile.changePage( "about/us.html", { data: {paramUno:"Uno", paramDos:11} });
Run Code Online (Sandbox Code Playgroud)
我的问题是,一旦us.html加载,我不确定访问这些值的最佳方法.我添加页面事件回调pageshow,pagebeforeshow,pageinit,和pagechange在任何情况下,event.data是undefined.什么是正确的方法来做到这一点?我宁愿不使用localStorage或global/namespaced var,除非这是我唯一的选择.
谢谢!
Gaj*_*res 11
像这样发送:
$.mobile.changePage('page2.html', { dataUrl : "page2.html?parameter=123", data : { 'paremeter' : '123' }, reloadPage : true, changeHash : true });
Run Code Online (Sandbox Code Playgroud)
并按如下方式阅读:
$("#index").live('pagebeforeshow', function (event, data) {
var parameters = $(this).data("url").split("?")[1];;
parameter = parameters.replace("parameter=","");
alert(parameter);
});
Run Code Online (Sandbox Code Playgroud)
更多示例可以在这里找到:jQuery Mobile:文档就绪vs页面事件,只需寻找章节:页面转换之间的数据/参数操作.
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>
</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
</script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
$(document).on('pagebeforeshow', "#index",function () {
$(document).on('click', "#changePage",function () {
$.mobile.changePage('second.html', { dataUrl : "second.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : false, changeHash : true });
});
});
$(document).on('pagebeforeshow', "#second",function () {
var parameters = $(this).data("url").split("?")[1];;
parameter = parameters.replace("parameter=","");
alert(parameter);
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="index">
<div data-role="header">
<h3>
First Page
</h3>
</div>
<div data-role="content">
<a data-role="button" id="changePage">Test</a>
</div> <!--content-->
</div><!--page-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
second.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>
</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
</script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="second">
<div data-role="header">
<h3>
Second Page
</h3>
</div>
<div data-role="content">
</div> <!--content-->
</div><!--page-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果您想了解有关此主题的更多信息,请查看本文.您将找到几个带有示例的解决方案.