简体   繁体   English

在页面上创建弹出窗口时,Home和End键不起作用?

[英]Home and End keys don't work when popup created on page?

If you take a look at this page you'll see a bunch of products in containers. 如果你看看这个页面,你会看到一堆容器中的产品。 If you click on one of the products, a popup will open that will (most likely) have a greater height than the browser window and so a scrollbar will be shown. 如果您单击其中一个产品,将打开一个弹出窗口(很可能)将具有比浏览器窗口更高的高度,因此将显示滚动条。

After a popup is opened, the Home and End keys do not make the element scroll when pressed. 打开弹出窗口后,Home和End键不会在按下时滚动元素。 However, if you click in the popup, then the keys work. 但是,如果单击弹出窗口,则键可以正常工作。

I've tried calling .focus() and .click() on the popup after opening it, but the Home and End keys still don't have any effect until I click in the popup with my mouse. 我已经打过电话.focus().click()打开它后弹出,但Home和End键仍然没有任何效果,直到我与我的鼠标在弹出的点击。

Why don't the Home and End keys make the element scroll when pressed, and how can I get them to work? 为什么Home和End键不会在按下时滚动元素,如何让它们工作?

focus() won't actually give keyboard focus to an element unless the element is focusable. 除非元素是可聚焦的,否则focus()实际上不会将键盘焦点赋予元素。 Interactive elements like input and textarea are focusable, but by default a div is not. inputtextarea等交互式元素是可聚焦的,但默认情况下div不是。 However, you can make it focusable by setting its tabIndex property (or tabindex attribute ). 但是,您可以通过设置tabIndex属性 (或tabindex属性 )使其可聚焦。

When I load your page and type the following into console: 当我加载您的页面并在控制台中键入以下内容时:

$(".productPopupContainer").prop("tabIndex", "-1");
$(".productPopupContainer").focus();

The Home and End keys begin to work even though I haven't clicked in the popup. 即使我没有点击弹出窗口,Home和End键也会开始工作。 (I'm using the latest version of Chrome on a Mac, btw). (我在Mac上使用最新版本的Chrome,顺便说一句)。

The jQuery documentation page for focus() has a good explanation of how this works. focus()jQuery文档页面很好地解释了它是如何工作的。

FUN FACT : If you don't set tabIndex on an element but read that property, you'll get -1: 有趣的事实 :如果你没有在元素上设置tabIndex但是读取该属性,你将获得-1:

var elem = document.getElementById("#anything");
console.log(elem.tabIndex); // prints -1

But internally in the browser, the value is actually not defined, because explicitly setting it to -1 makes it focusable, when it wasn't previously. 但是在浏览器内部,实际上没有定义该值,因为显式地将其设置为-1使得它可以在以前不可聚焦时进行聚焦。 This means you can actually do this to make an element focusable without affecting its tab order: 这意味着您可以实际执行此操作以使元素可聚焦而不会影响其Tab键顺序:

elem.tabIndex = elem.tabIndex;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM