Jquery mobile Phonegap和本地存储问题,但在浏览器中工作
我必须在我的phonegap(3.0)应用程序中使用localstorage,以便我可以在每个页面中拨打用户想要的特定号码。为了做到这一点,我使用了下面这样的表单和脚本。此脚本可在浏览器中工作,但不适用于phonegap生成。我应该错过什么,但找不到什么Jquery mobile Phonegap和本地存储问题,但在浏览器中工作,jquery-mobile,cordova,phonegap-build,Jquery Mobile,Cordova,Phonegap Build,我必须在我的phonegap(3.0)应用程序中使用localstorage,以便我可以在每个页面中拨打用户想要的特定号码。为了做到这一点,我使用了下面这样的表单和脚本。此脚本可在浏览器中工作,但不适用于phonegap生成。我应该错过什么,但找不到什么 <!DOCTYPE html> <html lang="fr"> <head> <title>local storage</title> <meta charse
<!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>
本地存储
#信息{
显示:无;
边界半径:10px;
填充:10px;
背景色:#ff0000;
颜色:#fff;
}
#存储演示第h2页{
空白:正常;
}
var localStorageKey=“allo”;
$('#storageDemoPage').live('pagecreate',function()){
showStoreValue();
$(“#添加到存储”)。单击(函数(e){
setItem(localStorageKey,$('#entry').val());
showStoreValue();
e、 预防默认值();
});
});
函数showStoreValue(){
var item=localStorage.getItem(localStorageKey);
如果(项==null){
项目='Pas de valeur';
}
else if(item.length==0){
项目='Aucune valeur d'enregistreée';
}
$('.storeItem')。文本(项目);
}
我的应用程序
- 输入您的号码
-
- 值记录
-
编辑:在开头添加cordova脚本以下是您可能会错过的内容 a) Cordova脚本文件包含在头标记中 b) 在使用本地存储之前,请使用并侦听设备就绪
实现这一点应该可以解决您的问题。多亏了@QuickFix和@Chris,以下是解决方案:
<script type="text/javascript">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
var localStorageKey = "allo";
function onDeviceReady() {
showStoreValue();
$('#addToStorage').click(function(e) {
window.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>
//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
var localStorageKey=“allo”;
函数ondevicerady(){
showStoreValue();
$(“#添加到存储”)。单击(函数(e){
window.localStorage.setItem(localStorageKey,$('#entry').val();
showStoreValue();
e、 预防默认值();
});
}
函数showStoreValue(){
var item=localStorage.getItem(localStorageKey);
如果(项==null){
项目='Pas de valeur';
}
else if(item.length==0){
项目='Aucune valeur d'enregistreée';
}
$('.storeItem')。文本(项目);
}
在phonegap中,在访问localstorage之前,尝试侦听deviceready而不是pagecreate。这不起作用,是不是你想的?var localStorageKey=“allo”;文件。添加的文件列表(“showStoreValue”,onDeviceReady,false)$('#storageDemoPage').live('ondevicerady',function(){showStoreValue();$('#addToStorage')。单击(function(e){localStorage.setItem(localStorageKey,$('#entry').val();showStoreValue();e.preventDefault();});嗨,克里斯,我的开发应用程序中有cordova脚本,你是对的,我应该在这里包括(我会更新问题)。我将尝试b解决方案,谢谢你的帮助