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

标签 javascript cordova

我使用的是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/

相关文章:

javascript - 打开虚拟键盘时我的应用程序 webview 不会缩小?

android - 找不到参数 [] 的方法 getCompileConfiguration()

仅在 Chrome 中出现 JavaScript 错误,在 Safari 中运行正常?

javascript - 动态轮播 slider HTML?

javascript - jQuery 获取显示为 :none property 的元素的高度

javascript - 无法生成 django javascript i18n 目录

javascript - 为 Javascript 显示文本启用多语言支持的推荐方法是什么

javascript - 使用javascript读取PDF文件

javascript - 在 Phonegap 或 Cordova 中发送多条短信

android - 使用 PhoneGap/Cordova 为 Android 构建时,在 Mac OS X 10.9 Mavericks 上执行命令 'ant' 时出错