[英]How can I prevent a <div> with overflow: auto from being scrolled when the user hits the space bar?
I have a web page with left and right DIVs, both set to overflow: auto with content such that both divs have vertical scroll bars. 我有一个带有左右DIV的网页,它们都设置为溢出:自动显示内容,这样两个div都具有垂直滚动条。
I use a jquery keyboard event handler attached to document to have SPACE keys and move the selection from one item in the right DIV to the next item in the right DIV. 我使用附加到文档的jquery键盘事件处理程序具有SPACE键,并将选择从右侧DIV中的一项移至右侧DIV中的下一项。 I have a table of options on in the left DIV. 我在左DIV中有一个选项表。 If I click on a link on the left, then hit space, the keyboard handler performs the correct operation in the right DIV, but the left DIV also scrolls down. 如果单击左侧的链接,然后单击空格,键盘处理程序将在右侧的DIV中执行正确的操作,但左侧的DIV也会向下滚动。 If I click on some blank space in the right DIV and hit space again, everything behaves correctly. 如果我单击右侧DIV中的一些空白区域并再次单击空格,则一切正常。
It is clear to me that overflowing DIVs (and presumably other block elements) have focus, but I can't find any discussion on how to manipulate this, nor can I find any information on blocking key events to default handlers. 对我来说很明显,溢出的DIV(可能还有其他块元素)具有重点,但是我找不到任何有关如何操作此方法的讨论,也找不到任何有关将键事件阻止给默认处理程序的信息。 I know there must be a way because Google Reader doesn't have the same problem, but so far I haven't been able to figure out what they are doing to get the behavior I desire. 我知道一定有办法,因为Google阅读器没有同样的问题,但是到目前为止,我还无法弄清楚他们正在做什么以达到我想要的行为。
event.preventDefault();
should do it (add it first in your handler function). 应该做到这一点(首先将其添加到您的处理函数中)。
http://api.jquery.com/event.preventDefault/ http://api.jquery.com/event.preventDefault/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.