phonegap - 菜单按钮不会触发任何东西

发布于 2024-12-09 19:18:05 字数 1086 浏览 0 评论 0原文

我有以下代码来测试菜单按钮:

<script type="text/javascript" cahrset="utf-8">     
    //wait till device is ready
document.addEventListener("deviceready", onDeviceReady(), false);

//add a menu button event handler
function onDeviceReady() {
    alert('deviceready');
    document.addEventListener("menubutton", onMenuKeyDown(), false);
}
    //fired when menubutton of device is clicked
function onMenuKeyDown() {
    alert('menu key down');
}

</script>

但是当我按下菜单按钮时,没有任何反应。 Logcat 没有说什么,但我想因为这是来自标签“web console”,所以它可能很有用:

10-13 15:46:16.720: INFO/Web Console(13969): Error in success callback: Network Status1 = TypeError: Cannot read property 'apply' of undefined at :-2167

我从 Phonegap API 参考,但它似乎无法处理我的菜单按钮按下操作。 有什么帮助吗?预先非常感谢,丹尼尔

编辑:也许这对您了解发生了什么有帮助:当我按下菜单按钮时,logcat

10-18 09:21:52.560: WARN/WindowManager(152): statePower =normal

也显示,事件“searchbutton”也不起作用

I have the following code to test the menu button :

<script type="text/javascript" cahrset="utf-8">     
    //wait till device is ready
document.addEventListener("deviceready", onDeviceReady(), false);

//add a menu button event handler
function onDeviceReady() {
    alert('deviceready');
    document.addEventListener("menubutton", onMenuKeyDown(), false);
}
    //fired when menubutton of device is clicked
function onMenuKeyDown() {
    alert('menu key down');
}

</script>

however when I press them menu button, nothing happens. Logcat says nothing, but I guess since this is from tag "web console" it might be useful:

10-13 15:46:16.720: INFO/Web Console(13969): Error in success callback: Network Status1 = TypeError: Cannot read property 'apply' of undefined at :-2167

I followed this Tutorial from Phonegap API Reference but it doesnt seem to handle my menu button presses.
any help? thanks a lot in advance, daniel

edit: Maybe this is helpful for you to know whats going on: When I press the menu Button, logcat shows

10-18 09:21:52.560: WARN/WindowManager(152): statePower =normal

also, the event "searchbutton" doesnt work either

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨曦慕雪 2024-12-16 19:18:05

据我所知,这似乎是听众的问题。通过在函数名称末尾添加括号,您可以在设置侦听器时立即调用该函数,而不是为稍后触发的事件设置引用。

尝试:

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

并且:

document.addEventListener("menubutton", onMenuKeyDown, false);

希望这有帮助!

From what I can see, it looks like an issue with your listeners. By adding the brackets at the end of the function name, you are invoking the function immediately when the listener is setup, instead of setting a reference for the event to fire later.

Try:

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

And:

document.addEventListener("menubutton", onMenuKeyDown, false);

Hope this helps!

活雷疯 2024-12-16 19:18:05

检查您的body标签。确保它调用onload()函数。像这样:

<body onload="onLoad()">

Check your body tag. Ensure that it calls the onload() function. Like this:

<body onload="onLoad()">
浪荡不羁 2024-12-16 19:18:05

好的,这个对我有好处

var menuOpen = true;
var menuDiv = "";
function onLoad(){
        document.addEventListener("deviceready", onDeviceReady, false);
        menuDiv = document.querySelector("#menu");
}
function onDeviceReady(){

            document.addEventListener("menubutton", onMenuKeyDown, false);
}
function onMenuKeyDown() {

                if(menuOpen) {
                    console.log("close the menu");
                    menuDiv.style.display="none";
                    menuOpen = false;
                } else {
                        console.log("open the menu");
                        menuDiv.style.display="block";
                        menuOpen = true;
                }
}

Ok, this one got good for me

var menuOpen = true;
var menuDiv = "";
function onLoad(){
        document.addEventListener("deviceready", onDeviceReady, false);
        menuDiv = document.querySelector("#menu");
}
function onDeviceReady(){

            document.addEventListener("menubutton", onMenuKeyDown, false);
}
function onMenuKeyDown() {

                if(menuOpen) {
                    console.log("close the menu");
                    menuDiv.style.display="none";
                    menuOpen = false;
                } else {
                        console.log("open the menu");
                        menuDiv.style.display="block";
                        menuOpen = true;
                }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文