6

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

我正在寻找一种从键盘激活菜单栏的方法。我尝试了下面的代码。Right 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));
4

2 回答 2

1

只有将 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");

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

所以我会尝试专注于一个按钮或锚项目。

于 2011-06-20T07:08:55.187 回答
0

尝试从打开菜单的函数内部对第一个菜单项调用 .focus() 。例如:

$("#menu").find("[role=menuitem]")[0].focus();

这有效,但有一个外观问题。当使用箭头键更改菜单选择时,会在第一项周围留下橙色焦点光环。除此之外,它似乎按预期工作

我尝试改用该.menu("focus")方法,但正如您可能已经发现的那样,焦点在片刻之后再次被带走——我认为是因为鼠标不在菜单区域内。

于 2015-10-07T02:29:55.003 回答