2012-04-18 71 views
0

我的總體目標是能夠使用TAB鍵盤鍵在3個div之間進行導航,每個div都具有#sectionA,#sectionB和#sectionC的CSS ID。並且在每個div中,我有一個無序列表,我希望使用右箭頭鍵和左箭頭鍵來瀏覽列表。如何嵌套多個keydown事件?

我的HTML標記是這樣的:

   <div id="sectionA"> 
      <ul id="ul_sectionA"> 
        <li> Section A - Test B</li> 
        <li> Section A - Test B</li> 
      </ul> 
    </div> 
    <div id="sectionB"> 
      <ul id="ul_sectionB"> 
        <li> Section B - Test B</li> 
        <li> Section B - Test B</li> 
      </ul> 
    </div> 
      <div id="sectionC"> 
      <ul id="ul_sectionC"> 
        <li> Section C - Test B</li> 
        <li> Section C - Test B</li> 
      </ul> 
    </div> 

到目前爲止,我能夠得到以下的jQuery代碼,但不工作,一旦我添加第二個(文檔)$ .keydown(()的函數代碼

$(document).ready(function() 
    { 
     var divs = ["sectionA","sectionB","sectionC"]; 
     var startIndex = 0; 

     $(document).keydown(function(e) 
     { 
      alert("test"); 
       if (e.which == 9) 
      { 
       $("div").css("border", ""); 
        $("#"+divs[startIndex]).css("border","1px solid blue"); 
       var divID = $("#" +divs[startIndex]).attr("id"); 
       $(document).keydown(function(event) 
       { 
        if(event.which == 37) 
        { 
         if("#"+divID+"ul li").addClass() == "highlight") 
         { 
          $(this).next().addClass("highlight"); 
         } else 
         { 
          $(this).addClass(); 
         } 
        } 
       }); 
       startIndex++; 

        if(startIndex === divs.length) 
        { 
          startIndex = 0;     
        } 
       } 
     }); 
    });  
+0

你爲什麼要嵌套?重構所以功能全部駐留在一個事件中... – 2012-04-18 00:35:20

+0

從來沒有聽說過重構。是否有工作示例 – 2012-04-18 00:46:21

+1

重構,如重新組織,重新編碼,重新設計。使用變量來跟蹤您正在使用哪個標籤和列表項;然後基於哪個選項卡處於活動狀態,根據所使用的箭頭鍵在該選項卡中選擇各種列表項。 – 2012-04-18 00:48:00

回答

0

希望這將讓你在正確的方向前進,這是不完美的任何手段。

你曾試圖巢keydown事件。你不能做到這一點的基本問題。研究所您需要爲右箭頭和左箭頭分別設置一個keydown事件,這些事件中的每一個都需要確定用戶「選擇了」哪個div,然後滾動該div中的那些列表項。下面的代碼將開始該過程,但它只處理左箭頭,甚至還沒有完成。

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     var divs = ["sectionA","sectionB","sectionC"]; 
     var startIndex = 0; 
     var startListItemIndex = 0; 
     var divID; 
     $(document).keydown(function(e) 
     { 
      if (e.which == 9) 
      { 

       $("div").css("border", ""); 

       $("#" + divID + " ul li").css("border", "0px"); //remove all borders 
       $("#"+divs[startIndex]).css("border","1px solid blue"); //now add border to selected div 
       divID = $("#" +divs[startIndex]).attr("id"); 

       startListItemIndex = 0; //reset startIndex each time tab is pressed. 
       startIndex++; 

       if(startIndex === divs.length) 
       { 
         startIndex = 0;     
       } 

      } 
       //left arrow click 
       if(event.which == 37) 
       { 
       startListItemIndex++; 

       //remove all highlights first 
       $("#" + divID + " ul li").removeClass("highlight"); 

       //now add highlight to the next li element. 
       $("#" + divID + " ul li:nth-child(" + startListItemIndex + ")").addClass("highlight"); 
       } 
     }); 


    }); 
    </script> 
+1

爲什麼2 keydown事件綁定?只要在第一個包含if語句。 – Lazerblade 2012-04-18 01:10:41

+0

好點,我更新了代碼並做出了改變。 – 2012-04-18 01:49:12