启用 div 无需单击即可通过键盘滚动 [英] Enable div to scroll by keyboard without clicking

查看:49
本文介绍了启用 div 无需单击即可通过键盘滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 div 元素,我在其中加载文本,通常是长文本,所以 div 有滚动条.我可以用鼠标在这个 div 中滚动,当光标在它上面时,但我不能上下滚动页面,除非我先在 div 内部单击.那么有没有办法避免这种点击,在加载文本时从代码中做到这一点?

I have div element in which I am loading text, usually long text, so the div has scrollbars. I can scroll in this div with mouse, when the cursor is on it, but I cant scroll with page up and down, unless I click inside the div first. So Is there a way to avoid this clicking, to do it from code when the text is loaded?

推荐答案

就如你所描述的那样.我将输入附加到内容 div,然后 focus() 它,而不是尝试聚焦 div.因此,在按下加载文本后,您可以立即按下 pg up/dn 滚动 div.

Here it is, as you described. I appended an input to the content div, then focus() it, rather than trying to focus the div. So, immediately after pressing load text, you can press pg up/dn to scroll div.

在这里

 $("div").append('<input type="text" id="focusDiv">');
    /*
 Here I need to have some code which gives "focus to the div, so if I press the load text and then immediately PageDown key, sthedvis scrolls down.

 $("button").on("click", function (e) {
    e.preventDefault();
    $("div").html("Very long text...");
    $("div").append('<input type="text" id="focusDiv">');
    $('#focusDiv').focus();
});

这篇关于启用 div 无需单击即可通过键盘滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆