最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable certain key's default action - Stack Overflow

programmeradmin5浏览0评论
function keypressCheck() {
    var keyID = event.keyCode;

    //space pressed
    if (keyID == 32) {
        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?

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

    //space pressed
    if (keyID == 32) {
        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?

Share Improve this question edited Feb 14, 2011 at 0:32 Tim Down 325k76 gold badges460 silver badges542 bronze badges asked Feb 13, 2011 at 22:39 ptigers9ptigers9 1091 gold badge2 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

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

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

评论列表(0)

  1. 暂无评论