2017-05-29 273 views
0

我想創建使用科爾多瓦,jQuery和jQuery的移動 我寫了這個代碼的Android應用包括所有的文件:jQuery與科爾多瓦?

<!DOCTYPE html> 
 
<!-- --> 
 
<html> 
 
    <head> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
     <meta charset="UTF-8"> 
 
     <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> 
 

 
     
 
     <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
 
     <script type="text/javascript" src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
 
     <script type="text/javascript" src="/js/myScript.js"></script> 
 
     <title></title> 
 
    </head>

jQuery的移動工作完美,但jQuery的不要」將不起作用我試圖把在index.js一些代碼在結束(這是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'); 
 
    }, 
 
    // 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();

,但它不工作,如果我寫在myScript.js

爲什麼如果jQuery的手機是好的,我不能使用jQuery一些代碼,它不工作過? Thx求助

回答

0

發生這種情況可能是因爲在deviceready啓動時DOM未完全加載。您可以使用下面的代碼來初始化應用程序:

var deviceReadyDeferred = $.Deferred(); 
    var jqmReadyDeferred = $.Deferred(); 

    document.addEventListener("deviceready", deviceReady, false); 

    function deviceReady() { 
     deviceReadyDeferred.resolve(); 
    } 

    $(document).on("mobileinit", function() { 
     jqmReadyDeferred.resolve(); 
    }); 

    $.when(deviceReadyDeferred, jqmReadyDeferred).then(init); 

    function init() { 
     // Your initialization code here 
    }