我是cordova和javascript的新手。我只是试图打印一些消息,并希望在我单击按钮时显示警报。但是一旦应用程序加载就会发生。
这是我的代码。
<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
document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage());
function showLocalStorage() {
document.getElementById("demo").innerHTML = "value is empty";
console.log('fsdfsd');
alert("am i amazing!!")
}
请指出我错误的地方。感谢。
答案 0 :(得分:8)
不要执行该函数,只需将其绑定到addEventListener并在单击时执行它。
更改此
document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage());
到这个
document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage);