admin管理员组文章数量:1386700
When focus is on a button in a JQuery dialog, the space bar does two things: it selects the button and it scrolls the window. People who are familiar with space bar as a tool for selecting buttons will expect the first but find the second jarring and inappropriate.
So the question: how do I prevent the page from scrolling? I had thought that it was just a matter of returning false from the button handler but that does not appear to be true.
When focus is on a button in a JQuery dialog, the space bar does two things: it selects the button and it scrolls the window. People who are familiar with space bar as a tool for selecting buttons will expect the first but find the second jarring and inappropriate.
So the question: how do I prevent the page from scrolling? I had thought that it was just a matter of returning false from the button handler but that does not appear to be true.
Share Improve this question asked Apr 14, 2012 at 20:36 Mark BrittinghamMark Brittingham 28.9k13 gold badges82 silver badges111 bronze badges1 Answer
Reset to default 8I've tried the solution suggested here and elsewhere and none of them worked for me. What finally worked was a document-wide keydown handler like this:
$(document).keydown(function (e) {
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if ((key == 32) && (e.target.className != null) && (e.target.className.indexOf("ui-button") != -1))
e.preventDefault();
});
The key == 32 is obviously a check for the space bar. The className is a check for whether the UI (User Interface) element in question is a JQuery button. Without the button check, the space bar is disabled everywhere.
本文标签:
版权声明:本文标题:javascript - How to prevent page from scrolling when a JQuery button is focused and space-bar is pressed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744554735a2612403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论