【发布时间】:2017-09-13 11:26:27
【问题描述】:
在科尔多瓦我想从监听器调用一个按钮事件。在我的应用程序中,我正在加载一个页面 http://example.org/app 。在那个页面有一个按钮,它的类名是 btn。我想在单击该按钮时显示警报。网页的html代码如下所示。
<p><a class="btn">Click</a></p>
index.js 中的cordova 代码
var app = {
// Application Constructor
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function () {
app.receivedEvent('deviceready');
// Here, we redirect to the web site.
var targetUrl = "http://example.org/app";
var bkpLink = document.getElementById("bkpLink");
bkpLink.setAttribute("href", targetUrl);
bkpLink.text = targetUrl;
window.location.replace(targetUrl);
//I want to call a alert when button click
document.getElementsByClassName('btn')[0].addEventListener('click', (function(i) {
return function() {
alert("okkkkkkkkkk");
};
})(i), false);
},
// Note: This code is taken from the Cordova CLI template.
receivedEvent: function (id) {
}
};
app.initialize();
当我使用此代码时,它不起作用。 如何监听加载网页的点击事件。请帮帮我。
【问题讨论】:
-
你定义了
i在点击事件中调用函数的变量吗? -
不,我没有定义 i 变量。
-
如果你想在 jquery 中做一个简单的函数,$('.btn').click(function(){ alert('okkkk')});
-
它在科尔多瓦工作吗?
-
尝试定义或删除,也可以直接制作
addEventListener('click', function() { alert('ok'); })
标签: javascript jquery asp.net cordova phonegap