工作jQuery代码不会在phonegap中运行 [英] working jQuery code won't run in phonegap

查看:153
本文介绍了工作jQuery代码不会在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) 。

Code works in jsfiddle (someone provided it to me) jsfiddle, I've edited it to integrate into phonegap, but the code now won't work when I run it in phonegap (Xcode simulator for iOS).

推荐答案

当您的 deviceReady 启动时,这些元素可能不存在。请从 deviceReady 中删除​​这些行,并按如下格式输入:

Those elements may not be there when your deviceReady fired.So remove those lines from deviceReady and write like following:

$(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>

这篇关于工作jQuery代码不会在phonegap中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆