【发布时间】:2015-11-21 13:51:57
【问题描述】:
我有以下 HTML:
...
</head>
<body>
<div class="app">
<h1>Match It!</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="camera">take a pic</button>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
...
我正在尝试在单击 ID 为 camera 的按钮时打开相机。
我在 index.js 中有以下 JavaScript:
var app = {
...
cameraUse: function() {
navigator.camera.getPicture(function(imagePath){
document.getElementById("photoImg").setAttribute("src", imagePath);
}, function(){
alert("Photo cancelled");
}, {
destinationType: navigator.camera.DestinationType.FILE_URI
});
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
document.getElementById("camera").addEventListener("click", cameraUse, false);
},
我希望在单击按钮时执行cameraUse 函数。
【问题讨论】:
-
您的上述错误,将
document.getElementById("camera").addEventListener("click", cameraUse, false);更改为document.getElementById("camera").addEventListener("click", app.cameraUse, false);您需要在cameraUse之前添加app. 参见:stackoverflow.com/questions/33778334/… - Javascript 不是Java。
标签: javascript ios cordova camera phonegap-build