cordova/phonegap onclick doesn't work

2019-02-26 08:51发布

I try to create button which starts an alert after a click event. Something like that :

    <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>
        <button onclick="alert('salut')" id="boutonAlerte">Alerte</button>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script> </body>

but the problem is that my onclick doesn't work. A lot of people tell me to try it on javascript so I've decided to write something like that :

function onAlertClick() {
  alert("ceci est une alerte");
}

alert("test2");

var bA = document.getElementById("boutonAlerte");
bA.addEventListener("onclick", onAlertClick());

But this is the same thing.

Any ideas ?

7条回答
爷、活的狠高调
2楼-- · 2019-02-26 09:20

onclick is not fired on cordova apps, because users don't "click", instead, they "tap". You may use a decent mobile framework like Ionic, and use ng-click, it automatically solves the problem for you, because it includes ngTouch.

If you prefer not to use a big framework like angular, you may use Zepto.js, and use $(".element").on('tap', callback); to listen for tap events. Or $(".element").on('tap click', callback); to listen for both tap and click events.

Chrome dev tools has good mobile emulation, to trigger tap events. It may be helpful if you want to debug you app in Chrome.

查看更多
登录 后发表回答