禁用某些键的默认操作 [英] Disable certain key's default action

查看:122
本文介绍了禁用某些键的默认操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function keypressCheck() {
    var keyID = event.keyCode;

    //space pressed
    if (keyID == 32) {
        anotherFunction();
    }
}

我想要$ code anotherFunction() 在空格键被按下而不滚动页面的默认动作发生时运行。有没有办法这样做?

I want anotherFunction() to run when the space bar is pressed without the default action of the page scrolling to happen. is there any way to do this?

推荐答案

它应该可以工作。只需要确保,请尝试:

It should work. Just to make sure, try this:

function keypressCheck(e) { 
    var e = window.event||e; // Handle browser compatibility
    var keyID = e.keyCode;
    //space pressed
    if (keyID == 32) {
        e.preventDefault(); // Prevent the default action
        anotherFunction();
    }
}

这篇关于禁用某些键的默认操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆