且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Firefox上按空格键时停用向下滚动

更新时间:2023-12-02 22:35:22

这应该可以做到。它说,当在页面/文档上按空格键时,它不仅仅防止其默认行为,而是还原到原始状态。

This should do the trick. It states that when the spacebar is pressed on the page/document it doesn't just prevent its default behavior, but reverts back to original state.

return false似乎包括preventDefault。 来源

return false seems to include preventDefault. Source

检查JQuery API了解关键事件事件的详情 - http://api.jquery.com/keydown/

Check JQuery API's for more information about keydown events - http://api.jquery.com/keydown/

window.onkeydown = function(e) { 
    return !(e.keyCode == 32);
};

JQuery示例

$(document).keydown(function(e) {
    if (e.which == 32) {
        return false;
    }
});

EDIT

As @ amber-de-black表示上述代码将阻止按HTML输入上的空格键。要解决这个问题,你 e.target 在这里你想要空格键阻塞。这可以防止空格键阻止其他元素(如HTML输入)。

As @amber-de-black stated "the above code will block pressing space key on HTML inputs". To fix this you e.target where exactly you want spacebar blocked. This can prevent the spacebar blocking other elements like HTML inputs.

在这种情况下,我们指定空格键和body目标。这将阻止输入被阻止。

In this case we specify the spacebar along with the body target. This will prevent inputs being blocked.

window.onkeydown = function(e) {
  if (e.keyCode == 32 && e.target == document.body) {
    e.preventDefault();
  }
};

注意:如果您使用JQuery,请使用 e.which 而不是 e.keyCode 来源

NOTE: If you're using JQuery use e.which instead of e.keyCode Source.

event.which属性规范化event.keyCode和event.charCode

JQuery作为各种事件的规范化器。如果这让任何人读到这个惊喜。我建议您阅读他们的事件对象文档。

JQuery acts as a normalizer for a variety of events. If that comes to a surprise to anyone reading this. I recommend reading their Event Object documentation.