gpt4 book ai didi

javascript - Phonegap 3 - 如何调用 Javascript 函数

转载 作者:行者123 更新时间:2023-11-28 01:22:36 25 4
gpt4 key购买 nike

我使用的是phonegap 3,但无法调用我的函数,它不起作用!

这是我的代码,它是创建联系人的基本示例,但我不会创建一堆按钮来执行不同的操作,例如创建、删除列表等类似的操作,但我无法使用它。唯一执行的代码位于“OnDeviceReady”函数内部。

我如何调用我的函数以及如何调用?因为基本的Javascript无法运行。谢谢!

索引.html

<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>PILLEpalle!</title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<div id="contactcreated" class="blink">
<p class="event listening">Creating Contact</p>
<p class="event received">Contact created</p>
</div>
</div>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>

我的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 explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
var myContact = navigator.contacts.create({"displayName": "Test User"});
myContact.note = "This contact has a note.";
console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
app.receivedEvent('contactcreated');
myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
},
// 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);
},
onContactSaveSuccess: function(contact) {
window.alert("Save Success");
},
onContactSaveError: function(contact) {
window.alert("Save Failed");
},
};

最佳答案

看来您需要阅读一些有关 POO 的内容:http://en.wikipedia.org/wiki/Prototype-based_programming

基本上,您在 var app 作用域内创建的函数如下所示:app.myFunction();

如果您希望在设备事件发生时调用 then,则需要将 then 添加为设备事件的监听器。看看文档:http://docs.phonegap.com/en/3.4.0/cordova_events_events.md.html#Events

关于javascript - Phonegap 3 - 如何调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23085950/

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