允许div通过键盘滚动而不点击

dav*_*vid 7 scroll

我有一个 div 元素,我在其中加载文本,通常是长文本,因此 div 有滚动条。当光标位于该 div 上时,我可以用鼠标在该 div 中滚动,但我无法上下滚动页面,除非我先在 div 内单击。那么有没有办法避免这种点击,在加载文本时通过代码来完成呢?

Tod*_*odd 1

正如您所描述的,它就是这样。我将一个输入附加到内容 div,然后 focus() 它,而不是尝试聚焦该 div。因此,在按下加载文本后,您可以立即按下 pg up/dn 来滚动 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();
});
Run Code Online (Sandbox Code Playgroud)