2013-11-01 1 views
2

버튼만으로 구성된 기본 웹 페이지를 만들었지 만 편리 성을 위해 편의성을 위해 키보드만으로 버튼을 액세스 할 수 있도록하고 싶습니다 ... 도와주세요. 이페이지의 모든 버튼을 키보드를 통해 액세스 할 수있게 만드는 방법

<table border="0" align="center" cellpadding="5px" cellspacing="5px"> 
<tr> 
    <td><button class="1" style="height:auto" onClick="#" onKeyDown="myFunction()">Milk Collection</button></td> 
</tr> 
<tr> 
    <td><button class="1" onClick="#")> Local Sale</button></td> 
</tr> 
<tr> 
    <td><button class="1" onClick="#")> Utilities</button></td> 
</tr> 
<tr> 
    <td><button class="1" onClick="#")> Master</button></td> 
</tr> 

+1

나는 당신의 브라우저가이 불평 유무를 알 수없는,하지만 당신은 속하지 않는 당신의 HTML에서 별도의 브라켓을 ;

+0

괜찮 았습니다. 질문 게시시 실수로 수행되었습니다. :) –

답변

1

이 여기에이 'shortcut' plugin

사용하면

window.onkeydown = function(e) { 
    var key = e.keyCode; 
    if (key === 13) { 
     //Enter key 
    } 
}; 
+0

덕분에 많은 일을했습니다 –

2

키보드 바로 가기를 만들 수있는 쉬운 방법을 도울 수있는 방법의 예 페이지의 코드 것입니다 그걸 써;

<script type="text/javascript" src="js/shortcut.js"></script> 

<script> 
    shortcut.add("alt+s", function() { 
     // Do something 
    }); 
    shortcut.add("ctrl+enter", function() { 
     // Do something 
    }); 
</script> 

제 3 자 플러그인 (jquery 제외)을 사용하지 않으려면 max max; 현재 키 누르기 이벤트는 Google 크롬이나 Safari에서는 작동하지 않지만 keydown을 사용하면 모든 브라우저에서 작동합니다.

$(window).keydown(function(e) { 
    var code = e.which || e.keyCode; //<--edit, some browsers will not give a keyCode 
    switch (code) { 
     case 37: case 38: //key is left or up 
      if (currImage <= 1) {break;} //if is the first one do nothing 
      goToPrev(); // function which goes to previous image 
      return false; //"return false" will avoid further events 
     case 39: case 40: //key is left or down 
      if (currImage >= maxImages) {break;} //if is the last one do nothing 
      goToNext(); // function which goes to next image 
      return false; //"return false" will avoid further events 
    } 
    return; //using "return" other attached events will execute 
}); 

사용하려는 키에 대한 keyCode을 찾기 위해, 당신은 단지 alert(e.keyCode); 위의 함수에서 다음 키 순서에 대한 케이스를 추가 할 수 있습니다.

+0

감사합니다 남자, 당신은 방금 1000 명 이상의 내 대표를 밀었습니다! : D – pythonian29033

+0

당신의 시간 동안 Thnx.하지만 여전히 나는 안합니다. 나의 현재 버튼 ** 밀크 세일 **은 focued되고 keydown의 언론에서 ** Local Sale **과 같은 다음 버튼으로 가야합니다. –

+0

어떤 솔루션을 사용하고 있으며 질문을 편집하고 코드를 추가 할 수 있습니까? 곧 살펴 보겠습니다. – pythonian29033

1

HTML :

<button class="1" style="height:auto" onkeydown="myFunction(event)">Milk Collection</button> 

자바 스크립트 :

function myFunction(e) { // Trigger the click event from the keyboard 
    if (e.keyCode == 13) { 
     alert("click"); 
     return false; 
    } 
} 
관련 문제