jQuery 与科尔多瓦?
Posted
技术标签:
【中文标题】jQuery 与科尔多瓦?【英文标题】:jQuery with Cordova? 【发布时间】:2017-10-29 21:54:26 【问题描述】:我想使用 Cordova、jQuery 和 jQuery-mobile 创建一个 android 应用程序 我写了这段代码来包含所有文件:
<!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-mobile 可以完美运行,但 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-mobile 很好,为什么我不能使用 jQuery? 谢谢帮忙
【问题讨论】:
【参考方案1】:这可能是因为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
【讨论】:
以上是关于jQuery 与科尔多瓦?的主要内容,如果未能解决你的问题,请参考以下文章