使用jquerymobile的时候,我们新进入一个页面可能需要添加一个返回按钮,用户返回到上一个页面,我们可以使用上一篇文章中的方法,在Header中添加返回上一个页面的链接,但是这样有一点不好,如果这个页面有多个入口(即可以从多个页面跳入本页面),那么我们这个返回就得做判断了。在这种情况下我们可以使用jquerymobile的data-add-back-btn属性,并将其值设为true。这样jquerymobile会自动为我们记住该返回哪个页面。下面看一个例子代码:
<!Doctype html>
<html>
<head>
<title>Phone Gap Introduce</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="../JS/jquery.mobile-1.3.2.min.css"/>
<link rel="stylesheet" type="text/css" href="../JS/jquery.mobile.structure-1.3.2.css"/>
<link rel="stylesheet" type="text/css" href="../JS/jquery.mobile.theme-1.3.2.css"/>
<script type="text/javascript" src="../JS/jquery.js"></script>
<script type="text/javascript" src="../JS/cordova.js"></script>
<script type="text/javascript" src="../JS/jquery.mobile-1.3.2.min.js"></script>
<script type="text/javascript">
$('#PageOne').live('pageinit', function(event){
var showTip = function(){
navigator.notification.alert("this is a message from page one!", null, "Message", "Close");
$(this).die("click");
};
var confirm = function(){
navigator.notification.confirm(
'You are the winner!', // message
null, // callback to invoke with index of button pressed
'Game Over', // title
'Restart,Exit' // buttonLabels
);
$(this).die("click");
};
var redirectPage = function(){
$.mobile.changePage("#PageTwo");
$(this).die("click");
};
$(event.target).find('#alert').bind('click', showTip);
$(event.target).find('#confirm').bind('click', confirm);
$(event.target).find('#changePage').bind('click', redirectPage);
});
$('#PageTwo').live('pageshow', function(event){
var showTip = function(){
navigator.notification.alert("this is a message from page two!", null, "Message", "Close");
$(this).die("click");
};
var confirm = function(){
navigator.notification.confirm(
'You are the losser!', // message
null, // callback to invoke with index of button pressed
'Game Over', // title
'Restart,Exit' // buttonLabels
);
$(this).die("click");
};
$(event.target).find('#alert').bind('click', showTip);
$(event.target).find('#confirm').bind('click', confirm);
});
</script>
</head>
<body>
<div id="PageOne" data-role="page">
<div data-role="header" data-backbtn="false">
<h1>Phone Gap One</h1>
</div>
<div data-role="content">
<div>
<a href="#" id="alert" data-role="button" data-theme="b">Alert</a>
</div>
<div>
<a href="#" id="confirm" data-role="button" data-theme="b">Confirm</a>
</div>
<div>
<a href="#" id="changePage" data-role="button" data-theme="b">Change Page</a>
</div>
</div>
<div data-role="footer">
<div data-role="navbar">
<ul>
<li><a href="#PageOne">Page One</a></li>
<li><a href="#PageTwo">Page Two</a></li>
</ul>
</div>
</div>
</div>
<div id="PageTwo" data-role="page">
<div data-role="header" data-backbtn="true">
<h1>Phone Gap Two</h1>
<a data-role="button" data-rel="back">Previous</a>
</div>
<div data-role="content">
<div>
<a href="#" id="alert" data-role="button" data-theme="b">Alert</a>
</div>
<div>
<a href="#" id="confirm" data-role="button" data-theme="b">Confirm</a>
</div>
<div>
<a href="file:///android_asset/www/Pages/pageThree.html" data-role="button" data-theme="b" data-prefetch>Page Three</a>
You can also <a href="#aboutPage">learn more</a> about Megacorp.
</div>
</div>
<div data-role="footer">
<div data-role="navbar">
<ul>
<li><a href="#PageOne">Page One</a></li>
<li><a href="#PageTwo">Page Two</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>