我有一些似乎没有被执行的脚本,但我不知道为什么,我正在尝试使用IOS模拟器和safari来调试它以查看日志。
以下是index.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>Test</title>
</head>
<body>
<div id="appContainer" class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script>
alert('Wait!'); // is not working
</script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
这是index.js的内容
alert("wait"); // That one is working, why ?
var app = {
// Application Constructor
initialize: function() {
console.log("app init"); // not working
this.bindEvents();
console.log("app init end"); // not working
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// "load", "deviceready", "offline", and "online".
bindEvents: function() {
console.log("bind start"); // not working
document.addEventListener("deviceready", this.onDeviceReady, false);
document.addEventListener("click", this.onClick, false);
console.log("bind end"); // not working
},
// 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() {
console.log("device start");
app.receivedEvent("deviceready");
document.getElementById("appContainer").addEventListener("click", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchstart", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchend", this.onClick, false);
console.log("device end");
},
// 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);
},
onClick: function(e) {
console.log("click"); // not working or it may be the eventlistener
},
};
console.log("init s"); // not working
app.initialize();
console.log("init e"); // not working
我不明白的是 1 - 为什么index.html中的警报(“等待”)不起作用。 2 - 为什么console.log无法运行设备启动,设备结束和已接收事件:deviceready 3 - 为什么app.initialize似乎没有被执行(因为没有日志)但实际上是因为设备就绪事件被触发而被执行 4 - 当我在模拟器中触发单击时,为什么我看不到onClick函数中的日志(如果设备就绪事件工作,单击,touchstart和touchend应该不工作?)
有人可以向我解释为什么似乎没有什么工作正常吗?
我正在使用cordova 5.3.1,几乎不需要修改init包。我试图删除ios并再次添加它但它没有改变任何东西。
感谢您的帮助
答案 0 :(得分:1)
“console”global是一个Apache Cordova插件。它只能 ondeviceready 。看看http://plugins.cordova.io/#/package/org.apache.cordova.console
试试这个
var app = {
initialize: function() {
console.log("app init");
this.bindEvents();
console.log("app init end");
},
// Bind Event Listeners
bindEvents: function() {
console.log("bind start");
document.addEventListener("click", this.onClick, false);
console.log("bind end");
},
onDeviceReady: function() {
console.log("device start");
app.initialize();
app.receivedEvent("deviceready");
document.getElementById("appContainer").addEventListener("click", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchstart", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchend", this.onClick, false);
console.log("device end");
},
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);
},
onClick: function(e) {
console.log("click"); // not working or it may be the eventlistener
},
};
//initialize app on device ready
document.addEventListener("deviceready", app.onDeviceReady, false);
至于你的“警示”之谜,window.alert兼容性在Cordova的背景下已被证明是不可靠的。我建议你使用
navigator.notification.alert
这是插件对话框https://www.npmjs.com/package/cordova-plugin-dialogs
的一部分另外,如果这是你的目标,有更好的方法来操纵DOM和CSS。