2017-02-24 1 views
-1

안녕하세요 예를시뮬레이션 프레스 키

<a href"http://google.es">Link</a> 

이 링크는 내가 예를 들어 누를 때 키보드의 숫자 5와 같은 키우면 것을 자바 스크립트 또는 유사한을 사용하는 방법이 있습니다 마우스로 링크를 누르면? 감사

+0

마우스 이벤트를 사용할 수 있습니다. –

답변

1

당신이 keypress 이벤트를 결합하고 event.which을 도청하고 해당 키를 누를 때 프로그래밍 방식으로 a 또는 링크 요소를 클릭 할 수 있습니다 jQuery를 $(document).keypress(function(event) { event.charCode == 53 ? window.open("google.es","_self") : console.log('another key'); });

1

를 사용할 수 있습니다.

예를 들어 CodePen을 확인하십시오. 참고 :이 예는 jQuery 플러그인을 사용하고 있습니다.

+0

감사합니다. 예를 들어 18을 누르면 9보다 높은 숫자를 사용할 수 있습니까? – htmlpower

+1

필자는 CodePen을 업데이트했습니다. 이제 숫자 1과 숫자 8이 눌 렸을 때 링크 클릭이 트리거됩니다. – Gaurav

+0

대단히 감사합니다. – htmlpower

1

시도해보십시오. jQuery 사용하기.

<html> 
<head> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script> 
    function checkKey(e) { 
     var code; 

     if (e.keyCode) code = e.keyCode; 

     if (code == 53) { // replace the 53 with the keycode of your choice. 
      $('#myLink').trigger("click"); 
     } 
    } 
</script> 
</head> 
<body onkeydown="checkKey(event);"> 
<button id="myLink" onclick="window.location.href= 'https://google.com'">Link</button> 
</body> 
</html> 

키 코드 참조 : http://www.foreui.com/articles/Key_Code_Table.htm

0

당신이 JQuery와의 키 누름 이벤트를 사용할 수 있습니다

$(function() { 
$(document).keypress(function(e) { 
    if (e.which === 53){ 
    location.href = "http://google.es"; 
    }   
    }); 
}); 

해당 키

의 키주고의 ASCI 값을 변경하려면이 키 5입니다