gpt4 book ai didi

javascript - onDeviceReady() 有效但 document.addEventListener ("deviceready", onDeviceReady(), false);失败。为什么?

转载 作者:行者123 更新时间:2023-11-30 08:49:34 25 4
gpt4 key购买 nike

下面是我的脚本:

<script>
var candata = {cantitle:"", candetail:"", imagesource:"", canlat:"", canlon:""};
var cantemplate = '<input type="text" name="cantitle" id="cantitle" value="{{cantitle}}" placeholder="can Title"/><textarea cols="40" rows="8" name="candetail" id="candetail" placeholder="can Detail">{{candetail}}</textarea><input id="addphoto" type="button" data-icon="plus" value="Add a Photo" /><img id="canimage" src="{{imagesource}}" /><div id="map">Map Placeholder</div><input id="savecan" type="button" data-theme="a" value="Save can" />';

$(document).on("pageinit", function(){
$("#newcan").on("pagecreate", function(){
var html = Mustache.to_html(cantemplate);
$("#candetailcontent").html(html);
});
});

function init() {
document.addEventListener("deviceready", onDeviceReady(), false);
}

function onDeviceReady() {
alert('Ready'); //Not being called
var networkstate = navigator.network.connection.type;
if(networkstate="none"){
$("#offline").css("visibility", "visible");
}
}
</script>

但是,如果我将文档的 addEventListener 更改为 document.addEventListener("deviceready", onDeviceReady, false);,则不会触发 onDeviceReady() 中的警报。为什么?

编辑

完整代码:为什么我的 onDeviceReady 没有触发?

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="format-detection" content="telephone-no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi">
<title>Our Application</title>
<link rel="stylesheet" href="css/themes/default/jquery.mobile-1.3.2.min.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.3.2.min.js"></script>
<script src="cordova.ios.js"></script>
<script src="js/mustache.js"></script>
<script>
var candata = {cantitle:"", candetail:"", imagesource:"", canlat:"", canlon:""};
var cantemplate = '<input type="text" name="cantitle" id="cantitle" value="{{cantitle}}" placeholder="can Title"/><textarea cols="40" rows="8" name="candetail" id="candetail" placeholder="can Detail">{{candetail}}</textarea><input id="addphoto" type="button" data-icon="plus" value="Add a Photo" /><img id="canimage" src="{{imagesource}}" /><div id="map">Map Placeholder</div><input id="savecan" type="button" data-theme="a" value="Save can" />';

$(document).on("pageinit", function(){
$("#newcan").on("pagecreate", function(){
var html = Mustache.to_html(cantemplate);
$("#candetailcontent").html(html);
});
});

function init() {
document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
alert('Ready'); //Not being called
var networkstate = navigator.network.connection.type;
if(networkstate="none"){
$("#offline").css("visibility", "visible");
}
}
</script>
</head>
<body onload="init()">
<div data-role="page" id="main">
<div data-role="header" class="logo"> <img src="img/logo.png" /> </div>
<div data-role="content">
<ul id="canlist" data-role="listview">
</ul>
<a href="#newcan" data-role="button" data-icon="plus">New can</a> </div>
<div data-role="footer" data-theme="c">
<h2 class="offline">Offline Mode</h2>
</div>
</div>
<div data-role="page" id="newcan">
<div data-role="header" class="logo"> <img src="img/logo.png" /> </div>
<div id="candetailcontent" data-role="content"> </div>
<div data-role="footer" data-theme="c">
<p>Snapcan!</p>
</div>
</div>
</body>
</html>

最佳答案

你的第一个函数声明:

函数初始化(){
document.addEventListener("deviceready", onDeviceReady(), false);

凭借 onDeviceReady 之后的括号,调用 函数并将返回值作为参数评估给 addEventListener,而第二个版本:

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

省略括号,因此是要调用函数的引用

关于javascript - onDeviceReady() 有效但 document.addEventListener ("deviceready", onDeviceReady(), false);失败。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19125136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com