[英]Call phonegap/cordova plugin function within html
我對javascript非常陌生,因此開始使用Phonegap進行一些練習。
我正在嘗試在html中調用一個插件函數,但是如果我在一個按鈕內調用它,則可以正常工作:
<button onclick="cordova.plugins.autoStart.disable()">Stop AutoStart</button>
而如果我嘗試直接調用它,則會在控制台中得到:“未定義autoStart”。
<script type="text/javascript">
cordova.plugins.autoStart.enable();
</script>
沒有按鈕,如何將其調用為html?
UPDATE1:
<!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 Desktop創建的,都應該已經創建了一個app.js文件。
只能在設備准備就緒后才能使用插件,因此您應該將代碼放在app.js的onDeviceReady()函數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.