本文介绍了Cordova:onclick事件在按钮单击之前被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Cordova和javascript的新手.我只是想打印一些消息,并想在单击按钮时显示警报.但是,一旦加载了应用程序,这种情况就会发生.
I am new to cordova and javascript. I am just trying to print some message and want to show alert when i click the button. But it is happening once the app is loaded.
这是我的代码.
<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 id = "setLocalStorage">SET LOCAL STORAGE</button>
<br/>
<button id = "showLocalStorage">SHOW LOCAL STORAGE</button><br/>
<button id = "removeProjectFromLocalStorage">REMOVE PROJECT</button><br/>
<button id = "getLocalStorageByKey">GET BY KEY</button>
<p id="demo"></p>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/myscript.js"></script>
</body>
JavaScript
Javascript
document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage());
function showLocalStorage() {
document.getElementById("demo").innerHTML = "value is empty";
console.log('fsdfsd');
alert("am i amazing!!")
}
请指出我犯错的地方.谢谢.
Please point me where i did mistake. Thanks.
推荐答案
不要执行该函数,只需将其绑定到addEventListener并在单击时执行即可.
Do not execute the function , just bind it to addEventListener and execute it when clicked.
更改此
document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage());
对此
document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage);
这篇关于Cordova:onclick事件在按钮单击之前被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!