Bootstrap Modal弹出窗口不能用键盘滚动(Bootstrap Modal popup not scrolling with keyboard)

编程入门 行业动态 更新时间:2024-10-17 05:02:02
Bootstrap Modal弹出窗口不能用键盘滚动(Bootstrap Modal popup not scrolling with keyboard)

我正在使用bootstrap模式,我添加了一个滚动条,它可以用鼠标正常工作,但是当我使用键盘时,只有背景页面滚动。 我该怎么做才能解决这个问题。 这是我正在使用的css代码。

.modal .modal-body { height: 520px; overflow-y: auto; }

我尝试了这段代码,但它不起作用

$('#myModal').keydown(function(e) { if(e.keyCode == 40){$('.modal-body').scrollTop($('.modal-body').scrollTop()+10);} if(e.keyCode == 38){$('.modal-body').scrollTop($('.modal-body').scrollTop()-10);} });

I'm using bootstrap modal, I added a scroll bar and it works fine with the mouse, but when I use the keyboard only the background page scrolls. What can I do to solve this. This is the css code that I'm using.

.modal .modal-body { height: 520px; overflow-y: auto; }

I tried this code but it doesn't work

$('#myModal').keydown(function(e) { if(e.keyCode == 40){$('.modal-body').scrollTop($('.modal-body').scrollTop()+10);} if(e.keyCode == 38){$('.modal-body').scrollTop($('.modal-body').scrollTop()-10);} });

最满意答案

击键只能由当前具有焦点的元素检测到,因此如果焦点位于背景中的元素上,则keydown事件将不会捕获任何内容。 我建议做以下事情:

通过赋予tabIndex=-1属性使您的模态可聚焦 将其设置为在打开时具有焦点,如下所示:$(“#my-modal”)。focus()

Keystrokes are only detected by the element that currently has the focus, so your keydown event won't trap anything if the focus is on an element in the background. I suggest doing the following:

Making your modal focusable by giving it the tabIndex=-1 attribute Setting it to have the focus when it opens, like this: $("#my-modal").focus()

更多推荐

本文发布于:2023-08-01 00:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不能用   弹出窗口   键盘   Modal   Bootstrap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!