在 html 内调用 phonegap/cordova 插件函数

Call phonegap/cordova plugin function within html

我是 javascript 的新手,我开始使用 Phonegap 进行一些练习。

我试图在 html 中调用插件函数,但如果我在按钮中调用它,它会正常工作:

<button onclick="cordova.plugins.autoStart.disable()">Stop AutoStart</button>

而如果我尝试直接调用它,我会在控制台中得到:"autoStart is not defined"。

<script type="text/javascript">
    cordova.plugins.autoStart.enable();
</script>

如何在没有按钮的情况下将其调入 html?

更新 1:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <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" />
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />

     <link rel="stylesheet" href="css/styles.css">
     <link rel="stylesheet" href="css/jquery-ui.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/jquery-1.11.2.min.js"><\/script>')</script>        
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

    <script type="text/javascript" src="cordova.js"></script>
</head>
<body>

    <button onclick="cordova.plugins.autoStart.disable()">Stop AutoStart</button>
    <div class="perspective">
        <img id="box1"/>
        <img id="box2"/>
    </div>

    <p id="date"></p>
    <p id="time"></p>

    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
        jQuery(document).ready(function () {
            //THE ERROR IS HERE
            cordova.plugins.autoStart.enable();
        });
    </script>        
</body>
</html>

当您创建 cordova 项目时,无论是通过命令行界面还是 Phonegap 桌面,都应该已经创建了一个 app.js 文件。

插件只能在设备准备就绪后使用,因此您应该将代码放在 app.js.

的 onDeviceReady() 函数中