2012-11-15 38 views
0

我有一個下拉菜單,我需要我鍵盤訪問,但不知道如何去做。在我的CSS我有一個:專注於所有的鏈接。我假設在javascript/jquery中使用onfocus會是最好的,而不是將tab鍵硬編碼到JavaScript中,對嗎?如果是這樣,我會怎麼做代碼來使用焦點選擇器?當有人選中它時,菜單需要放棄,然後當它們退出時(或者他們可以按下退出鍵,因爲有超過40個鏈接),它需要關閉(需要像mousingover/mousingout那樣)。 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> 
+0

如果你想添加的鍵盤按鈕,它 – ryanc1256

+0

它可能是有益的看看如何你可以看看http://api.jquery.com/keydown/其他鍵盤可訪問的菜單系統可以做到這一點像[superfish](http://users.tpg.com.au/j_birch/plugins/superfish/) – steveax

+0

好吧,我終於找到了一個在線編輯工作的代碼。如果有人想測試它,看看你是否可以使用鍵盤下降菜單,我將不勝感激。 我已經把VIDesignz的解決方案放到了html中,但是我無法讓它工作。 http://jsbin.com/udobuc/3/ – user1666190

回答

1

嘗試使用tabIndex的鏈接。 (但steveaz指出他們應該已經可調焦)

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

對於你不得不以及添加事件作爲tabIndex菜單。 $menu是你的菜單(S),我不知道那是什麼,調整到自己需要的選擇:

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

真的不建議用tabindex沾污。 – steveax

+0

@steveax:從SitePoint節省下來:_it不會導致兼容性問題,並且在所有測試過的瀏覽器中都有出色的支持_ – elclanrs

+0

無論何時您更改默認的標籤排序順序,參見[WCAG 2.0 - 聚焦次序](http://www.w3.org/TR/UNDERSTANDING-WCAG20/navigation-mechanisms-focus-order.html)。菜單中的鏈接已經可以調整,不需要添加任何內容。 – steveax

0

我會嘗試像...給你的按鈕欄Tab鍵索引然後綁定對焦/模糊函數來顯示和隱藏的按鈕欄菜單

$(document).load(function(){ 

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

}); 
+0

這是第二個問題,我不知道代碼需要做什麼。我已經試過把我認爲是從鼠標的現有代碼,但它不起作用(這是上面)。 – user1666190

+0

@ user1666190我根據您發佈的腳本編輯了我的答案,我相信這是您需要的工作。 – VIDesignz

+0

我是否將該代碼放置在上面的代碼底部或什麼?我已經在上面的代碼和HTML的頭部嘗試過,但它仍然不能在我的本地副本上工作。今晚我會更新上面鏈接的例子。 – user1666190