工作 jQuery 代码不会 运行 在 phonegap
working jQuery code won't run in phonegap
Js:
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
document.getElementById("btnSave").addEventListener("click",saveData,false);
document.getElementById("btnShow").addEventListener("click",showData,false);
}
function saveData(){
var data = window.localStorage.getItem("date");
var dates = data ? JSON.parse(data) : [];
dates.push( new Date() );
window.localStorage.setItem("date", JSON.stringify(dates));
alert("Your data is stored");
}
function showData() {
var data = JSON.parse(window.localStorage.getItem("date"));
console.log(data);
$('#res').html(JSON.stringify(data));
$(this).html('Update result');
}
html:
<button id="btnSave"> Save Data </button>
<button id="btnShow"> Show Data </button>
代码在 jsfiddle 中工作(有人提供给我)jsfiddle,我已经编辑它以集成到 phonegap 中,但是当我 运行 它在 phonegap 中时代码现在不起作用(Xcode 模拟器 iOS)。
当您的 deviceReady
fired.So 从 deviceReady
中删除这些行并像下面这样写时,这些元素可能不存在:
$(document).on("click","#btnSave",function(){
saveData();
});
$(document).on("click","#btnShow",function(){
showData();
});
并使用以下 jQuery
和 jQueryMobile
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
Js:
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
document.getElementById("btnSave").addEventListener("click",saveData,false);
document.getElementById("btnShow").addEventListener("click",showData,false);
}
function saveData(){
var data = window.localStorage.getItem("date");
var dates = data ? JSON.parse(data) : [];
dates.push( new Date() );
window.localStorage.setItem("date", JSON.stringify(dates));
alert("Your data is stored");
}
function showData() {
var data = JSON.parse(window.localStorage.getItem("date"));
console.log(data);
$('#res').html(JSON.stringify(data));
$(this).html('Update result');
}
html:
<button id="btnSave"> Save Data </button>
<button id="btnShow"> Show Data </button>
代码在 jsfiddle 中工作(有人提供给我)jsfiddle,我已经编辑它以集成到 phonegap 中,但是当我 运行 它在 phonegap 中时代码现在不起作用(Xcode 模拟器 iOS)。
当您的 deviceReady
fired.So 从 deviceReady
中删除这些行并像下面这样写时,这些元素可能不存在:
$(document).on("click","#btnSave",function(){
saveData();
});
$(document).on("click","#btnShow",function(){
showData();
});
并使用以下 jQuery
和 jQueryMobile
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>