我使用jquery mobile 1.4.2
我必须把id从一页引用到另一页。
html
<div data-role="page">
<div data-role="header">
hello</div>
<div data-role="content">
<a href="#freeadd" id="ofrecer" data-role="button" data-mini="true">Ofrezco en Venta</a>
<a href="#freeadd" id="ofrecer" class="aliq" data-role="button" data-mini="true">Ofrezco en Alquiler</a>
<a id="busco" href="#freeadd" data-role="button" data-mini="true">Buscar</a>
</div>
</div>
<div data-role="page" id="freeadd">
<div data-role="header">
display id
</div>
</div>
在下一页我想显示的标题取决于id(而不是显示id)。
存在以下几种解决方案:
解决方案1:
可以使用changePage:
传递值$.mobile.changePage('page2.html', { dataUrl : "page2.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : true, changeHash : true });
然后这样读:
$(document).on('pagebeforeshow', "#index", function (event, data) {
var parameters = $(this).data("url").split("?")[1];;
parameter = parameters.replace("parameter=","");
alert(parameter);
});
例子:
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>
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>
解决方案2:
或者您可以为存储目的创建一个持久的javascript对象。只要ajax用于页面加载(并且页面不以任何方式重新加载),该对象将保持活动状态。
var storeObject = {
firstname : '',
lastname : ''
}
示例:http://jsfiddle.net/Gajotres/9KKbx/
解决方案3:
您还可以像这样访问上一页的数据:
$(document).on('pagebeforeshow', '#index',function (e, data) {
alert(data.prevPage.attr('id'));
});
prevPage对象保存完整的上一页。
解决方案4:
作为最后一个解决方案,我们有一个漂亮的HTML实现的localStorage。它只适用于HTML5浏览器(包括Android和iOS浏览器),但所有存储的数据通过页面刷新是持久的。
if(typeof(Storage)!=="undefined") {
localStorage.firstname="Dragan";
localStorage.lastname="Gaic";
}
示例:http://jsfiddle.net/Gajotres/J9NTr/
找到更多关于他们在。
到另一个页面的链接在哪里?我只看到链接到特定的页面部分。
传递id为url param: www.example.com?id=45
if (!empty($_GET['id']) { // will check if argument exists and check if it's not === 0
$title = $db->getTitleById((int) $_GET['id']);
}
if (empty($title)){ // Will check if there was id param passed and entry was found in DB
$title = 'Some default title';
}