Cordova.js已关联,设备已未启动

时间:2016-06-29 19:37:28

标签: javascript html cordova

我已经连接了cordova.js,我有app.initialize();,并且设备准备好了。谁知道为什么?

JS:

var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        document.getElementById("finishbutton").addeventlistener("click", greet)
    },
    function greet(){
        alert("The button works.")
    }

};

app.initialize();

HTML:

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <button id = "finishbutton">Finish</button>
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
        app.initialize();
        </script>
    </body>
</html>

如果设备已经启动,我应该可以按下完成按钮并获取警报。相反,它只是锁定“连接到设备”。

1 个答案:

答案 0 :(得分:0)

试试这个:

  var app = {
        initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function() {
            app.receivedEvent('deviceready');
            document.getElementById("finishbutton").addEventListener("click", this.greet); 
    /*here you were missing the ";" and "Event" and "Listener" should be 
capitalized */
            },
            greet: function(){
              alert("The button works."); /*here you were missing the ";", too. additionally i think you should use greet: function() */
            }

        };