简体   繁体   English

如何使下拉菜单键盘可访问?

[英]How can I make my drop down menu keyboard accessible?

I have a drop down menu that I need i to be keyboard accessible, but don't know how to do it. 我有一个下拉菜单,需要我可以通过键盘对其进行访问,但不知道该怎么做。 In my css I have the a:focus on all the links. 在我的CSS中,我对所有链接都使用a:focus。 I'm assuming using onfocus in the javascript/jquery would be the best instead of hard coding the tab key into the javascript, correct? 我假设在javascript / jquery中使用onfocus将是最好的方法,而不是将Tab键硬编码到javascript中,对吗? If so, what would I do to the code to use the focus selector? 如果是这样,我将如何处理使用焦点选择器的代码? The menu needs to drop when someone tabs into it, then when they tab off of it (or maybe they could press the escape key since there are over 40 links) it needs to shut (needs to be just like it does when mousingover/mousingout). 菜单需要在有人跳入菜单时放下,然后在他们跳出菜单时放下(或者因为有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"; }
};


};

The html is as follows: 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>

Try using tabIndex for the links. 尝试将tabIndex用于链接。 (but as steveaz points out they should be already focusable) (但正如steveaz指出的那样,它们应该已经聚焦)

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

For the menu you'd have to add an event as well as the tabIndex . 对于菜单,您必须添加一个事件以及tabIndex $menu is the selector for your menu(s), I'm not sure what that is, adjust to your needs: $menu$menu的选择器,我不确定是什么,请根据您的需要进行调整:

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

I would try something like...Give your buttonbar a tab index then bind focus/blur functions to show and hide the buttonbar menu 我会尝试类似...给按钮栏一个标签索引,然后绑定焦点/模糊功能以显示和隐藏按钮栏菜单

$(document).load(function(){

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

});

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

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