科尔多瓦 iOS 按钮事件

cordova iOS button event

本文关键字:按钮 事件 iOS      更新时间:2023-09-26

我正在尝试制作一个简单的 cordova 应用程序,并且我正在尝试包含一个按钮事件,当用户单击它时,会弹出一个警报。然而,到目前为止我没有任何运气。我很确定我已经为警报安装了所有正确的插件,但仍然没有。

这是我的js代码:

   var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // 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');
        window.analytics.startTrackerWithId('');
        window.analytics.trackView('Hello World');
        

    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');
        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
        console.log('Received Event: ' + id);
    },
    

    };
    app.initialize();

这是 HTML:

  <html>
    <head>
        
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <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">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>hello</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Jump on it</p>
                <p class="event received">Party on wayne</p>
            </div>
            <div>
                 <button id="myBtn" onclick="click()">click</button>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript"> function click()=    {alert("clicked");}</script>
    </body>
</html>

如果有人能帮忙,我将不胜感激。

你不调用你的点击函数,这就是问题所在。一个简单的例子:

<button id="myBtn" onclick="app.click()">Click me</button>

如果你的应用程序变得更加复杂,你应该在你的javascript中有一个事件侦听器,那么就不需要内联javascript了。

下一个:我,你打电话给像你的跟踪器这样的外部来源,你需要白听这个来源。