遍历左右箭头键

发布于 2025-01-23 04:54:41 字数 1269 浏览 2 评论 0原文

首先按“选项卡键”,第一个链接将突出显示,然后按右箭头键,以查看焦点跳到下一个级别的导航遍历前进,一旦我单击左箭头键焦点,必须返回。

在以下代码中,密码39工作正常。我按下键代码37后,有人可以帮助我。

fiddle 中的完整html代码。

<script>
$('.we-mega-menu-ul>.dropdown-menu a').keydown(function (e) {
if ([37, 39].indexOf(e.keyCode) === -1) {
    return;
}
var link = $(this);
switch (e.keyCode) {
    case 37: // left arrow key
    // Make sure to stop event bubbling
    e.preventDefault();
    e.stopPropagation();
    if (link.parent('li').find('li').filter(':visible').first().length === 0) {
        link.parent('li').find('li').filter(':visible').last().find('a').first().focus();
    }
    else {
        link.parent('li').find('li').filter(':visible').first().find('a').first().focus();
    }
    break;
    case 39: // right arrow key
    e.preventDefault();
    e.stopPropagation();
    if (link.parent('li').find('li').filter(':visible').first().length === 0) {
        link.parent('li').find('li').filter(':visible').last().find('a').first().focus();
    }
    else {
        link.parent('li').find('li').filter(':visible').first().find('a').first().focus();
    }
    break;
}
});
</script>

First press the tab key, the first link will be highlighted then press the right arrow keys to see the focus jumping to next level navigation traversing ahead, similar way once I click the left arrow key focus has to come back.

In the following code keycode 39 is working fine. Can someone help me once i press keycode 37 the focus has to go back.

Full html code in fiddle.

<script>
$('.we-mega-menu-ul>.dropdown-menu a').keydown(function (e) {
if ([37, 39].indexOf(e.keyCode) === -1) {
    return;
}
var link = $(this);
switch (e.keyCode) {
    case 37: // left arrow key
    // Make sure to stop event bubbling
    e.preventDefault();
    e.stopPropagation();
    if (link.parent('li').find('li').filter(':visible').first().length === 0) {
        link.parent('li').find('li').filter(':visible').last().find('a').first().focus();
    }
    else {
        link.parent('li').find('li').filter(':visible').first().find('a').first().focus();
    }
    break;
    case 39: // right arrow key
    e.preventDefault();
    e.stopPropagation();
    if (link.parent('li').find('li').filter(':visible').first().length === 0) {
        link.parent('li').find('li').filter(':visible').last().find('a').first().focus();
    }
    else {
        link.parent('li').find('li').filter(':visible').first().find('a').first().focus();
    }
    break;
}
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文