2016-11-29 87 views
0

我是新來的科爾多瓦和JavaScript。我只是試圖打印一些消息,並希望當我點擊按鈕時顯示警報。但是一旦應用程序被加載,它就會發生。科爾多瓦:onclick事件調用按鈕點擊之前

這是我的代碼。

<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!!") 
} 

請點我在哪裏,我沒有錯。謝謝。

回答

2

不要執行該函數,只需將它綁定到addEeventListener並在單擊時執行它。

更改此

document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage()); 

這個

document.getElementById("showLocalStorage").addEventListener("click", showLocalStorage);