Cordova / Phonegap把Javascript放在哪里

时间:2016-02-20 04:53:26

标签: javascript android cordova phonegap-build

我正在学习使用Cordova / Phonegap制作基于网络的移动应用程序,并且无法确定在哪里放置我的javascript代码。当你制作一个示例应用程序时,你得到一个模板,但我不知道从哪里开始我自己的代码。例如,假设我想写一个简单的行:

document.getElementById("someText").innerHTML = "Sample";

我不知道放在哪里。以下是他们提供的示例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');
    },
    // 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);
    }
};

app.initialize();

任何有经验的人都能帮帮忙吗?

1 个答案:

答案 0 :(得分:1)

创建了cordova项目后,在www目录中必须放置所有内容。在www目录中,app.js是脚本文件,您可以在其中编写问题中显示的脚本。在js/app.js页面中关联index.html,然后在

中清除代码
   receivedEvent: function(id) {
      // Code
   }

并将您的脚本放入

  receivedEvent: function(id) {
    // Your code
  }


  app.initialize();

 // Your functions
 $(document).ready(function() {
    // Your jQuery functions
 });