jQuery UI 菜单栏 : how to activate from keyboard

标签 jquery jquery-ui menu keyboard-shortcuts jquery-ui-menu

来自 jqueryui.com git master 的 jQuery UI 菜单栏允许键盘导航(如果处于事件状态)。

我正在寻找一种从键盘激活菜单栏的方法。我尝试了下面的代码。 右 Alt/AltGr 键被捕获。 然而,菜单中的箭头键仍然被忽略。 看起来应该打开第一个栏菜单板以使键盘导航生效或类似的东西。 如何从键盘激活菜单,以便无需单击鼠标即可使用键盘?

    <head><script type="text/javascript">
    $(function () {
    $(document).bind('keyup', function (event) {
       var keycode = (event.keyCode ? event.keyCode : event.which);
     if (keycode === 17) { // 17 = Right ALT / AltrGR
         $("#bar1").focus();
     }
     });
     </script></head>


    <body>
    <div id="container">
    <div id="bar1">
        <% Html.RenderAction("Menu", "Home"); %>
    </div>
    <div id="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </div>
    </div>
    </body>

更新

根据 Pehmolelu 的回答,看起来应该激活菜单栏中的第一个子菜单。 jQuery UI 测试的 Menubar.js 代码如下。如何找到并激活第一个菜单栏以便可以使用键盘按键进行导航?

/*
 * jQuery UI menubar
 *
 * backported from Michael Lang's fork:        
 http://www.nexul.com/prototypes/toolbar/demo.html
 */
(function($) {

// TODO take non-menubar buttons into account
$.widget("ui.menubar", {
options: {
  buttons: false,
  menuIcon: false
},
_create: function() {
  var self = this;
  var items = this.items = this.element.children("button, a");
  var o = this.options;

  this.element.addClass('ui-menubar ui-widget-header ui-helper-clearfix');

items.next("ul").each(function(i, elm) {
        $(elm).menu({
            select: function(event, ui) {
                ui.item.parents("ul:last").hide()
                self.options.select.apply(this, arguments);
            }
        }).hide().keydown(function(event) {
            var menu = $(this);
            if (menu.is(":hidden")) 
                return;
            event.stopPropagation();
            switch (event.keyCode) {
            case $.ui.keyCode.LEFT:
                self.left(event);
                event.preventDefault();
                break;
            case $.ui.keyCode.RIGHT:
                self.right(event);
                event.preventDefault();
                break;
            case $.ui.keyCode.TAB:
                self[ event.shiftKey ? "left" : "right" ]( event );
                event.preventDefault();
                break;
            };
        });
    });
    items.each(function() {
        var input = $(this),
               menu = input.next("ul");

        input.bind("click focus mouseenter", function(event) {
            event.preventDefault();
            event.stopPropagation();
            if (menu.is(":visible") && self.active && self.active[0] == menu[0]) {
                self._close();
                return;
            }
            if (menu.length && (!/^mouse/.test(event.type) || self.active && self.active.is(":visible") )) {
                self._open(event, menu);
            }
        })
        .addClass("ui-button ui-widget ui-button-text-only ui-menubar-link")
        .wrapInner("<span class='ui-button-text'></span>");
        self._hoverable(input)

        if (o.menuIcon) {
            input.addClass("ui-state-default").append("<span class='ui-button-icon-secondary ui-icon ui-icon-triangle-1-s'></span>");
            input.removeClass("ui-button-text-only").addClass("ui-button-text-icon-secondary");
        }

        if (!o.buttons) {
            input.addClass('ui-menubar-link').removeClass('ui-state-default');
        };          

    });
    $(document).click(function(event) {
        !$(event.target).closest(".ui-menubar").length && self._close();
    });
},

_close: function() {
    this.items.next("ul").hide();
    this.items.removeClass("ui-state-active");
},

_open: function(event, menu) {
    if (this.active) {
        this.active.menu("closeAll").hide();
        this.active.prev().removeClass("ui-state-active");
    }
    var button = menu.prev().addClass("ui-state-active");
    this.active = menu.show().position({
        my: "left top",
        at: "left bottom",
        of: button
    }).focus();
},

left: function(event) {
    var prev = this.active.prevAll( ".ui-menu" ).eq( 0 );
    if (prev.length) {
        this._open(event, prev);
    } else {
        this._open(event, this.element.children(".ui-menu:last"));
    }
},

right: function(event) {
    var next =  this.active.nextAll( ".ui-menu" ).eq( 0 );
    if (next.length) {
        this._open(event, next);
    } else {
        this._open(event, 
this.element.children(".ui- menu:first"));
    }
}
});

}(jQuery));

最佳答案

只有将 tabindex 属性赋予 Div 元素才能获得焦点。

<div id="bar1" tabindex="1">

但是 div 元素根据定义与 tabindex 并不真正兼容:http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex

您可能应该尝试关注该 div 内的其他元素,看看它是如何工作的。

编辑:

在完全不了解实际菜单栏的情况下,我有一种预感。一开始的定义是:

var items = this.items = this.element.children("button, a");

因此,您选择所有按钮和 anchor 作为项目。然后在下面有这个 items.each,您可以在其中将单击焦点和 mouseenter 事件绑定(bind)到每个项目。

所以我会尝试关注按钮或 anchor 项目。

关于jQuery UI 菜单栏 : how to activate from keyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164415/

相关文章:

javascript - 我该如何实现? $example2.arctext({半径 : 400, 方向:-1})

Javascript - 从文件夹中获取图像

javascript - 如何将日期分配给 jquery ui datepicker?

javascript - event.preventDefault 和 stopPropagation 在拖/放事件中未按预期工作

ios - jQuery Mobile 页面内容在第一次访问时未加载

javascript - (图形)树结构中的 jQuery sortables

android - 如何将项目添加到子菜单中的组

html - CSS 下拉子菜单宽度

android - appcompat-v7 r21/r22 : force show overflow menu icon

javascript - 使用构造函数和调用函数