简体   繁体   English

我该如何预防 <div> 与溢出:当用户点击空格键时自动滚动吗?

[英]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.

相关问题 当正在滚动的内容位于设置为溢出的固定div内时,如何在jquery中执行滚动命令:滚动 - How to execute a scroll command in jquery when what's being scrolled is within a fixed div set to overflow: scroll 当用户下载文件时,如何防止Internet Explorer信息栏出现? - How can I prevent the Internet Explorer Info Bar from appearing when a user downloads a file? 如何检测到具有溢出的div已滚动到底部? - How do I detect that a div with overflow has scrolled to the bottom? 当用户单击“后退”按钮时,如何加载项目? - How can I load a project when the user hits the back button? 如何确定 div 是否滚动到底部? - How can I determine if a div is scrolled to the bottom? 延迟后,如何使div滚动到后消失? - After a delay, how can I make a div disappear after being scrolled to? 当特定div命中或距顶部100像素时,我如何才能使固定标头滚动走 - How can i make my fixed header scroll away when a specific div hits it, or when it is 100px from the top 有没有一种方法可以计算出您将div内的溢出设置为auto滚动了多少? - Is there a way to calculate how far you have scrolled inside a div with overflow set to auto? 滚动到窗口上方时如何将div /导航栏粘贴到窗口顶部 - How to stick a div/navigation bar to the top of the window when scrolled past it 当用户点击Enter时,如何防止清除文本字段 - How do I prevent clearing out of a textfield when the user hits enter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM