【问题标题】:How can I make my drop down menu keyboard accessible?如何使我的下拉菜单键盘可访问?
【发布时间】:2012-12-15 00:29:56
【问题描述】:

我有一个下拉菜单,我需要我可以通过键盘访问,但不知道该怎么做。在我的 css 中,我将 a:focus 放在所有链接上。我假设在 javascript/jquery 中使用 onfocus 是最好的,而不是将 tab 键硬编码到 javascript 中,对吗?如果是这样,我将如何处理代码以使用焦点选择器?当有人进入菜单时,菜单需要放下,然后当他们关闭菜单时(或者他们可以按退出键,因为有超过 40 个链接)它需要关闭(需要就像鼠标悬停/鼠标悬停时一样)。 Example Menu

if (ie === "1") {
if ($(window).width() > 949) {

$.fn.pause=function(a){$(this).stop().animate({dummy:1},a);return this};
function mouseleft(){$("#buttonbar").triggerHandler("mouseleave")}
$(document).ready(function()
  {$("#buttonbar").mouseenter(function(){$(this).stop().pause(160).animate({height:"12.7em"},400,"easeOutQuart")}).mouseleave(function(){$(this).stop().pause(160).animate({height:"2.2em"},400,"easeOutQuart")});});$(function(){$("#accordion").accordion({fillSpace:!0,icons:{header:"accordion-header",headerSelected:"accordion-headerselected"}})});
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
    $("#buttonbar li").bind('touchstart', function(){
        console.log("touch started");
    });
$("#buttonbar li").bind('touchend', function(){
        console.log("touch ended");
    });
}
else {
 $("#accordion").accordion({active:false});
}
}

function showElement(d){
  var s=document.getElementById(d).style;
  if ($(window).width() < 950) {
    if (s.display != "block" ) { s.display = "block"; } else { s.display = "none"; }
  if ($(window).width() > 949) {
    if (s.display != "block" ) { s.display = "block"; } 
  }
}
}
function showHide() {
  var s=document.getElementById("buttonbar").style;
  if ($(window).width() > 949) {
    s.display = "block";
    document.getElementById("prospective-links").style.display = "block";
    document.getElementById("current-links").style.display = "block";
    document.getElementById("academic-links").style.display = "block";
    document.getElementById("facstaff-links").style.display = "block";
    document.getElementById("parent-links").style.display = "block";
    document.getElementById("alumni-links").style.display = "block";
    document.getElementById("visitor-links").style.display = "block";
$("#accordion").accordion('destroy');
    $("#buttonbar").unbind('mouseenter');
    $("#buttonbar").unbind('mouseleave');
$.fn.pause=function(a){$(this).stop().animate({dummy:1},a);return this};
function mouseleft(){$("#buttonbar").triggerHandler("mouseleave")}
$(document).ready(function()
  {$("#buttonbar").mouseenter(function(){$(this).stop().pause(160).animate({height:"12.7em"},400,"easeOutQuart")}).mouseleave(function(){$(this).stop().pause(160).animate({height:"2.2em"},400,"easeOutQuart")});});$(function(){$("#accordion").accordion({fillSpace:!0,icons:{header:"accordion-header",headerSelected:"accordion-headerselected"}})});
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
    $("#buttonbar li").bind('touchstart', function(){
        console.log("touch started");
    });
$("#buttonbar li").bind('touchend', function(){
        console.log("touch ended");
    });
}

  }
  else {
    /*$("#accordion").accordion({active:false});*/
    $("#accordion").accordion('destroy');
    $("#buttonbar").unbind('mouseenter');
    $("#buttonbar").unbind('mouseleave');
    $("#buttonbar li").unbind('touchstart');
    $("#buttonbar li").unbind('touchend');

    s.display = "none";
    document.getElementById("prospective-links").style.display = "none";
    document.getElementById("current-links").style.display = "none";
    document.getElementById("academic-links").style.display = "none";
    document.getElementById("facstaff-links").style.display = "none";
    document.getElementById("parent-links").style.display = "none";
    document.getElementById("alumni-links").style.display = "none";
    document.getElementById("visitor-links").style.display = "none";
    /*$("#buttonbar").accordion('destroy');*/

  }

}
$(window).resize(showHide); //Run when resized

jQuery(function($) {
  var path = location.pathname.substring(1);
  $('#nav a[href$="' + path + '"]').addClass('active');
});

// This section makes the skip to anchor links such as the skip to content link work in webkit browsers like Chrome and Safari
(function(linkList,i,URI) {
   if (!!(URI = document.documentURI)) {
        URI = URI.split('#')[0];
      document.addEventListener("DOMContentLoaded", function() {
         document.removeEventListener("DOMContentLoaded", arguments.callee, false);
         linkList = document.links;
         for (i in linkList) {
            if (!!linkList[i].hash) {
               if (linkList[i].hash.match(/^#./)) {
                  if ((URI+linkList[i].hash)==linkList[i].href){
                     linkList[i].addEventListener("click", function(e,f,g) {
                        f = document.getElementById(this.hash.slice(1));
                        if (!(g = f.getAttribute('tabIndex'))) f.setAttribute('tabIndex',-1);
                        f.focus();
                        if (!g) f.removeAttribute('tabIndex');
                     }, false);
                  }
               }
            }
         }
      }, false);
   }
   return true;
})();
}
else {
function showElement(d){
  var s=document.getElementById(d).style;
  if (s.display != "block" ) { s.display = "block"; } else { s.display = "none"; }
};


};

html如下:

    <div id="buttonbar">
    <ul id="bar">
  <li id="prospective" class="rightborder"><a>Future Students</a>
    <ul id="prospective-links">
      <li><a href="/admissions">Undergraduate Admissions</a></li>
      <li><a href="/graduate">Graduate Admissions</a></li>
      <li><a href="/admissions/international">International Admissions</a></li>
      <li><a href="/transfer">Transfer Admissions</a></li>
      <li><a href="/advising-center">Academic Advising Center</a></li>
      <li><a href="/financial_aid">Financial Assistance</a></li>
      <li><a href="/info/schedules/tuition">Tuition and Fees</a></li>
      <li><a href="#">Test Item 8</a></li>
      <li class="last-item"><a href="/prospective">Go to Future Students</a></li>
    </ul></li>
      <li id="students" class="rightborder"><a href="/current">Current Students</a>
         <ul id="current-links">
            <li><a href="/info/calendars/academic">Academic Calendar</a></li>
            <li><a href="/info/schedules">Enrollment Schedule</a></li>
            <li><a href="/studentservices">Student Life</a></li>
            <li><a href="/housing">Housing</a></li>
            <li><a href="/financial_aid/workstudy.html">On-Campus Jobs</a></li>
            <li><a href="#">Bookstore</a></li>
            <li><a href="#">Athletics</A></li>
            <li class="fill"><a name="hold">&nbsp;</a></li>
            <li class="last-item"><a href="/current">Go to Current Students</a></li>
         </ul></li>
    </ul>
    </div>

【问题讨论】:

  • 如果你想在其中添加键盘按钮,你可以查看api.jquery.com/keydown
  • 看看其他键盘可访问菜单系统是如何做到这一点的可能会有启发性。喜欢superfish
  • 好的,我终于找到了一位在线编辑器来处理代码。如果有人想测试它以查看是否可以使用键盘删除菜单,我将不胜感激。我已经将VIDesignz的解决方案放到了html中,但是我不能让它工作。 jsbin.com/udobuc/3

标签: javascript jquery drop-down-menu accessibility


【解决方案1】:

尝试使用tabIndex 作为链接。 (但正如 steveaz 指出的那样,它们应该已经可以聚焦了)

$('#buttonbar a').attr('tabIndex', 0);

对于菜单,您必须添加一个事件以及tabIndex$menu 是您菜单的选择器,我不确定那是什么,请根据您的需要进行调整:

$('#bar').attr('tabIndex', 0)
  .focus(function() { $menu.show(); })
  .keydown(function(e) { e.which == 27 && $menu.hide(); }) // ESC
  .blur(function() { $menu.hide(); });

【讨论】:

  • 真的不推荐使用tabindex。
  • @steveax:它已保存,来自 SitePoint:它不会导致兼容性问题,并且在所有经过测试的浏览器中都具有出色的支持
  • 任何时候更改默认的跳格顺序,都可能弊大于利。见WCAG 2.0 - Focus Order。菜单中的链接已经可以聚焦,无需添加任何内容。
  • @steveax 我明白你在说什么......你能提供一个解决方案来帮助 OP 吗?除了表格和类似的简单东西外,我对制表的经验并不多。
  • 是的,链接应该是可聚焦的,但我认为菜单的tabIndex 没有任何问题。只要你使用tabIndex=0 就可以了。
【解决方案2】:

我会尝试类似...给你的按钮栏一个标签索引,然后绑定焦点/模糊功能来显示和隐藏按钮栏菜单

$(document).load(function(){

    $('#buttonbar').attr('tabIndex', 0).on({
       focus: function(){
          $("#buttonbar").triggerHandler("mouseenter");
       },
       blur: function(){
          $("#buttonbar").triggerHandler("mouseleave");
       }
    });

});

【讨论】:

  • 这是第二个问题,我不知道需要什么代码来做。我尝试从鼠标的现有代码中输入我认为的内容,但它不起作用(如上)。
  • @user1666190 我编辑了我的答案,根据你发布的脚本,我相信这是你让它工作所需要的。
  • 我是将该代码放在上面代码的底部还是什么?我已经尝试过上面的代码和 html 的头部,但它仍然无法在我的本地副本上运行。今晚我才能从上面的链接更新示例。
  • @user1666190 这应该在$('document').load(); 函数中,我将更新我的答案以反映这一点......
  • @user1666190 另外,我的代码中遗漏了一个括号……我的错 :( 现已修复!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 2020-04-27
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多