阅读和写作的localStorage? [英] Reading and writing from localStorage?
问题描述
我刚开始研究的PhoneGap + jQuery Mobile的和HTML5 ,,所以不要用我的白痴失去你的神经!
I just started to study Phonegap + jQuery Mobile and HTML5,, so don't loose your nerve with my idiocy!
有人能告诉我这是为什么不工作?当我使用从localStorage的一个变量,我就当pressing按钮,但使用的是变温=100,当得到一个空的屏幕,这是工作的罚款。搭载Android 4.1。
Could somebody tell me why this is not working? When I am using a variable from localStorage, I just get an empty screen when pressing the button, but when using a variable temperature="100", it is working fine. Android 4.1.
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.localStorage.setItem("temperature", "100");
var temperature = window.localStorage.getItem("temperature");
}
</script>
<div data-role="content">
<p>Working or not?</p>
<button onclick="myFunction()">Try it</button>
<p id="testi"></p>
<script type="text/javascript">
function myFunction() {
//var temperature = "100";----> This is working!
document.getElementById("testi").innerHTML = temperature;
}
</script>
</div>
另外一个问题:如何处理页面之间的变量的Windows Phone?他们不支持本地存储,所以有另一种方式来处理这个,如果你还没有得到DB连接?
Another question: How to handle variables between pages in Windows Phone? They are not supporting localStorage, so is there another way to handle this if you haven't got db-connection?
谢谢!
萨米
推荐答案
温度
是本地的范围 onDeviceReady
功能。也就是说,一旦功能结束,就不见了。
temperature
is local to the scope of the onDeviceReady
function. That is, once the function is over, it's gone.
您有两种选择:
// Make it global
var temperature;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.localStorage.setItem("temperature", "100");
temperature = window.localStorage.getItem("temperature");
}
或
// Retrieve it in myFunction
function myFunction() {
var temperature = localStorage.getItem("temperature");
document.getElementById("testi").innerHTML = temperature;
}
有关的功能范围的例子一个很好的列表,请尝试这个答案。
For a good list of examples of function scope, try this answer.
这篇关于阅读和写作的localStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!