2016-07-04 85 views
0

我試圖把一個警報在科爾多瓦的按鈕,我不明白爲什麼它不工作。 我用控制檯開始了一個新的科爾多瓦空白項目。 見下面我的代碼:科爾多瓦警報按鈕

的index.html

<!DOCTYPE html> 
<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"> 
     <title>Hello World</title> 
    </head> 
    <body> 
     <button id = "textlocal">Test Local</button>` 

     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 

index.js:

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicitly call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
     document.getElementById("textlocal").addEventListener("click", textLocal); 

    }, 
    // 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); 
    }, 

    function textLocal(){ 
      alert('test'); 
     } 
}; 
app.initialize(); 

也許我textLocal()函數是不是在正確的地方,但我不能形成如何解決它。 非常感謝您的耐心和您的幫助。

回答

0

代碼中存在JavaScript錯誤。請現在嘗試此代碼。也許你需要將「textLocal」監聽器添加到「deviceready」監聽器。

代碼

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicitly call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
     document.getElementById("textlocal").addEventListener("click", this.textLocal); 

    }, 
    // 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); 
    }, 
    textLocal: function() { 
     alert('Test'); 
    } 
}; 
app.initialize(); 
+0

謝謝你的回答,你的評論很有趣,我也認爲這個功能可能是這樣寫的,但是我嘗試了你的解決方案,但它不工作。 – Alex72

0

您的代碼有語法錯誤。

如果您不與按鈕關聯,此功能永遠不會調用。

您需要放置一個onclick監聽器。您可以使用的onclick標籤是這樣的:

<button id="textlocal" onclick="textLocal()">Test Local</button>(無空格)

或使用jQuery

$("#textlocal").on("click", textLocal);

+0

嗨,我試過,但它不工作。 – Alex72

0

從頭標記刪除這一點,並嘗試

<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 *"> 

如果仍然是其不工作,您的deviceready函數在DOM加載之前執行。所以你應該把它放在$(document).ready中,如果你正在使用jquery或者只是在onload事件。順便說一句,添加addEventListener對於這樣簡單的操作並不是好的方法,只需使用onclick監聽器即可。

+0

我也試過這個,但沒有結果。 – Alex72

+0

它不可能。也許你想用你自己的方式進行編碼。但是如果這段代碼不運行意味着你想要實現超越javascript和dom概念的東西。 –

+0

我明白了,但我不這麼認爲,我只是想知道如何在一個新的cordova項目中調用一個函數,而我首先從一個簡單的提醒函數開始。我想我沒有做正確的事情...... – Alex72

0

我允許自己帶一個有趣的方式來理解它爲什麼不起作用。我不太熟悉,科爾多瓦的發展,但我創建Visual Studio中的科爾多瓦項目,並得到這個:

的index.html

<button id="textlocal" data-role="button">Alert</button> 

index.js(在ondevice準備):

$('#textlocal').click(textLocal); 

test.js:

function textLocal() { 
alert('test');} 

,該溶液的作品。但是,我不想在visual studio上工作,但直接使用命令提示符並在任何編輯器外部工作。 這和我上面的要求有些相同,但仍然找不到讓它發生的方法。希望可以幫助你回答我的問題。 謝謝。