功能未在按键上运行点击电话间隙

Function not running on button click on phonegap

本文关键字:电话 电话间 间隙 运行 功能      更新时间:2023-09-26

我有以下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函数在单击按钮时执行。

你下载了cordova相机插件吗?如果没有,请下载并尝试你的代码

使用
适用于cordova版本5.0+

cordova插件添加cordova插入相机

对于旧版本

cordova插件添加org.apache.cordova.camera

将我的javascript更改为:

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    document.getElementById("camera").addEventListener("click", function() {
        navigator.camera.getPicture(function(imagePath){
            document.getElementById("photoImg").setAttribute("src", imagePath);
        }, function(){
            alert("Photo cancelled");
        }, {
            destinationType: navigator.camera.DestinationType.FILE_URI
        });
    }, false);
}

不同之处在于,我将负责打开相机的代码直接移到了addEventListener调用中,而不是将其放在app变量中自己的函数中。