PhoneGap/Cordova v5 addEventListener click事件不等待单击 [英] PhoneGap/Cordova v5 addEventListener click event not waiting for click

查看:82
本文介绍了PhoneGap/Cordova v5 addEventListener click事件不等待单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好研究员, 我是PhoneGap的新手,已经很多年没有编码了,并且下面的addEventListener有问题.

Hello Fellow Developers, I am new to PhoneGap, have not coded in a number of years, and have an issue with the addEventListener below.

我在下面的javascript中的addEventListener中采样的警报始终显示,即使用户没有单击按钮,也会调用该事件并显示警报.我想确保仅在实际单击按钮时才显示警报.

The alert which i am sampling in the addEventListener in the javascript below is being displayed always, even if the user does not click the button, the event is called and the alert is displayed. I would like to ensure the alert is only displayed when the button is actually clicked.

您能提供的任何帮助都会很棒.

Any help that you can provide would be awesome.

欢呼 德雷(Dre)

var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
onDeviceReady: function() {
alert("PhoneGap is working in index.js onDeviceReady");
var buttonTwo = document.getElementById("btnPageTwo");

// the below event listener is fired always, even without the user clicking or performing any action the alert is displayed
buttonTwo.addEventListener('click', alert("Button Clicked"), false);
app.receivedEvent('deviceready');
},

// 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>
<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">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<title>Multipage Document Lesson 09</title>
</head>
<body>
<p>First Page</p>
<button id="btnPageTwo">Go to Page 2</button>
</body>
</html>

推荐答案

您直接在添加事件中使用了警报.改用功能

You are using alert directly in add event. use function instead

buttonTwo.addEventListener('click',function(){ alert("Button Clicked");}, false);

这篇关于PhoneGap/Cordova v5 addEventListener click事件不等待单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆