PhoneGap / Cordova v5 addEventListener点击事件不等待点击

时间:2015-08-08 11:02:38

标签: javascript html cordova click

Hello Fellow Developers, 我是PhoneGap的新手,多年没有编码,并且下面的addEventListener存在问题。

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

您可以提供的任何帮助都很棒。

干杯 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>

1 个答案:

答案 0 :(得分:0)

您正在添加活动中直接使用提醒。改用函数

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