android phoneGap 静态页面中简单的数据传递

最终效果:


 

 

主要采用方式:

 

window.localStorage.setItem("xxxx","");

window.localStorage.getItem("xxxx");

 

比较简单

 

 

核心文件:

 

index.html

<!DOCTYPE HTML>

<html>
<head>
<title>PhoneGap_dataTransfer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<link rel="stylesheet" href="../css/jquery.mobile-1.0.1.min.css">
<script type="text/javascript" charset="utf-8" src="../js/jquery-1.7.1.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/phonegap.js"></script>

<script type="text/javascript">
	$(function(){
		
		//点击登录
		var signIn = function(){
			var str_username = $("#input_username").val();
			var str_password = $("#input_password").val();
			//保存用户名和密码
			window.localStorage.setItem("username",str_username);
			window.localStorage.setItem("password",str_password);
			
			$.mobile.changePage("about.html", {transition:"slide"});
		}
		
		$('#btn_signIn').bind('click',signIn);
		
	});
</script>

</head>

<body>
	<div>
		用户名:<input id="input_username"/>
	</div>
	<div>
		密  码:<input id="input_password" type="password" />
	</div>
	<div>
		<button id="btn_signIn" type="button">登录</button>
	</div>
</body>

</html>

 

about.html

<!DOCTYPE HTML>

<html>
<head>
<title>about</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
</head>

<body>
	<div data-role="page">
	<!-- 要把script脚本写在这里面,不能写在head中,因为真正的page从这里开始 -->
	 <script type="text/javascript">
		$(function() {
			var str_username = window.localStorage.getItem("username");
			var str_password = window.localStorage.getItem("password");
			$("#lab_username").text(str_username);
			$("#input_password").val(str_password);
		});
	</script>
		<div data-role="header">
	      <h1>欢迎您</h1>
	   </div>
	   <div data-role="content" >
	   		<label id="lab_username"></label><br>
	   		<!-- 这里要小心,不能写input_password了,因为在index.html中也有一个,会冲突 -->
			您的密码是:<input id="about_input_password"/>
		</div>
	</div>
</body>

</html>

 

 

正如代码内注释处,要注意的地方:

 

1. <script>要写在page的后面。

2. id的标识最好不要相同了,不同的页面中不要用和其他页面相同的id。这也就是有别于平常的html不同处之一(可能是div作为了page的关系吧)。

3. 不可存放安全保密数据,只能作为简单的静态页面间的数据交互。

 

 

如果要和后台服务器数据交互该怎么办呢?用Plugin的形式吗?或是还有更好一点的方法?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值