我必须在我的phonegap(3.0)应用程序中使用localstorage,以便我可以在每个页面中调用用户希望的具体数字。为了做到这一点,我使用下面的表单和脚本。这个脚本在浏览器中工作,但不能用phonegap构建。我应该错过一些东西,但找不到什么!Phonegap和LocalStorage问题,但在浏览器中工作
<!DOCTYPE html>
<html lang="fr">
<head>
<title>local storage</title>
<meta charset="utf-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<style>
#message {
display: none;
border-radius: 10px;
padding: 10px;
background-color: #ff0000;
color: #fff;
}
#storageDemoPage h2 {
white-space: normal;
}
</style>
<script type="text/javascript">
var localStorageKey = "allo";
$('#storageDemoPage').live('pagecreate', function() {
showStoreValue();
$('#addToStorage').click(function(e) {
localStorage.setItem(localStorageKey, $('#entry').val());
showStoreValue();
e.preventDefault();
});
});
function showStoreValue() {
var item = localStorage.getItem(localStorageKey);
if (item == null) {
item = 'Pas de valeur';
}
else if (item.length === 0) {
item = 'Aucune valeur d\'enregistrée';
}
$('.storeItem').text(item);
}
</script>
</head>
<body>
<section data-role="page" id="storageDemoPage">
<section data-role="header">
<h2>My app</h2>
</section>
<section data-role="content">
<p id="message"/>
<ul data-role="listview" data-inset="true">
<li data-role="list-divider">Enter your number</li>
<li>
<input type="text" id="entry" name="entry" placeholder="Write digits"/>
<input type="button" id="addToStorage" value="Store value"/>
</li>
<li data-role="list-divider">Value record</li>
<li class="storeItem"/>
</ul>
<div data-role="content">
<p id="audio_position"></p>
<ul id="allRepos" data-role="listview"></ul>
</div>
</section>
</body>
</html>
编辑:在开始时
在PhoneGap的尝试,你访问的localStorage之前听deviceready而不是pagecreate。 – QuickFix
这不起作用的是你的想法? var localStorageKey =“allo”; document.addEventListener(“showStoreValue”,onDeviceReady,false); 。 $( '#storageDemoPage')生活( 'onDeviceReady',函数(){ showStoreValue(); $( '#addToStorage')点击(函数(E){ localStorage.setItem(localStorageKey,$( ')。val()); showStoreValue(); e.preventDefault(); }); }); –