Sam*_*ami 8 javascript html5 android cordova
我刚开始学习Phonegap + jQuery Mobile和HTML5,所以不要因为我的愚蠢而放松你的神经!
有人可以告诉我为什么这不起作用?当我使用localStorage中的变量时,按下按钮时我只得到一个空屏幕,但是当使用变量温度="100"时,它工作正常.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>
Run Code Online (Sandbox Code Playgroud)
另一个问题:如何在Windows Phone中处理页面之间的变量?它们不支持localStorage,如果没有db-connection,还有另一种方法可以处理吗?
谢谢!
萨米
bry*_*mck 14
temperature是onDeviceReady函数范围的本地.也就是说,一旦功能结束,它就消失了.
您有两种选择:
// Make it global
var temperature;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.localStorage.setItem("temperature", "100");
temperature = window.localStorage.getItem("temperature");
}
Run Code Online (Sandbox Code Playgroud)
要么:
// Retrieve it in myFunction
function myFunction() {
var temperature = localStorage.getItem("temperature");
document.getElementById("testi").innerHTML = temperature;
}
Run Code Online (Sandbox Code Playgroud)
有关功能范围示例的详细列表,请尝试此答案.
| 归档时间: |
|
| 查看次数: |
17317 次 |
| 最近记录: |