2016-07-04 91 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上工作,但直接使用命令提示符并在任何编辑器外部工作。 这和我上面的要求有些相同,但仍然找不到让它发生的方法。希望可以帮助你回答我的问题。 谢谢。