javascript - PhoneGap 1.5 "menubutton"和 "searchbutton"与 jQuery Mobile

标签 javascript jquery android jquery-mobile cordova

我正在玩我的第一个 PhoneGap/Android 应用程序,但无法从 PhoneGap/Cordova 事件触发 $.mobile.changePage()

我已经更新了代码,而之前它只会触发一次。现在它根本不会触发,但更接近它可能应该如何完成。

这是我的代码:

    $(document).on('pageinit', function() {
        document.addEventListener("searchbutton", onSearchKeyDown, false); //never fires
        document.addEventListener("menubutton", onMenuKeyDown, false); //never fires
        alert("triggered"); //does fire
    });

    // search button press
    function onSearchKeyDown() {
        //change to search page
        alert("search");
        $.mobile.changePage("#page4", {transition: "pop"});
    }

    //menu button press
    function onMenuKeyDown() {
        //change to start page
        alert("menu");
        $.mobile.changePage("#page1", {transition: "pop"});
    }

最佳答案

使用pageInit(),而不是$(document).ready()

参见文档 here :

The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event. This event is explained in detail at the bottom of this page.

关于javascript - PhoneGap 1.5 "menubutton"和 "searchbutton"与 jQuery Mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775295/

相关文章:

jquery - 更改 z-index 和宽度但部分清除 sibling 不重叠?

java - 在 Android SDK 中导入文本文件

android - java.lang.IllegalStateException : Trying to create a platform view of unregistered type: com. pichillilorenzo/flutter_inappwebview 问题

javascript - 在指令内部的 Controller 之间交换信息

javascript - 在 Jquery 中为新节点添加自定义数据属性不起作用

javascript - extjs 4 门户示例

javascript - jquery 延迟加载和其他 Jquery/javascript 函数在附加后不起作用

javascript - .addClass().removeClass() 的问题

javascript - 如何像流畅的播放列表一样链接播放多个 HTML5 音频文件?

Android 6.0 Marshmallow 如何禁用/隐藏 Navigationbar/SystemUI