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()更多推荐
发布评论