我有一些脚本似乎没有执行但我不知道为什么,我正在尝试使用 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 中的 alert("Wait") 不起作用。 2 - 为什么 console.log 不工作执行设备启动、设备结束和接收到的事件:deviceready 3 - 为什么 app.initialize 似乎没有被执行(因为没有日志)但实际上被执行了,因为设备就绪事件被触发 4 - 为什么我在模拟器中触发点击时在 onClick 函数中看不到日志(如果设备就绪事件有效,点击、touchstart 和 touchend 应该无效?)
有人可以向我解释为什么似乎没有什么能正常工作吗?
我使用的是 cordova 5.3.1,几乎没有对 init 包进行任何修改。我尝试删除 ios 并再次添加它,但它没有任何改变。
感谢您的帮助
最佳答案
“控制台”全局是一个 Apache Cordova 插件。它只会在设备就绪时可用。看这里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。
关于javascript - Cordova 脚本未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32826857/