ERB 페이지에 Bootstrap (btn 클래스)이있는 버튼으로 스타일이 지정된 여러 개의 link_to 태그가 있습니다. Enter 키를 그중 하나에 어떻게 바인딩 할 수 있습니까?
"Enter 키를 그 중 하나에 바인딩"이라고 말하면 "사용자가 Enter 키를 누르면 내가 선택한 링크를 따라갑니다"를 의미한다고 가정합니다.
이를 위해 따라 가려는 링크 태그에 ID를 넣습니다. 아래의 예에서는#follow-me
그런 다음 Javascript를 사용해야합니다. 다음과 같이 jQuery를 사용한다고 가정합니다.
var ENTER = 13; //This is the js keycode for ENTER
function my_custom_keypress(e){
key = e.which;
if(key == ENTER){
var my_target_url = $('a#follow-me').attr('href'); //Figures out where your link goes...
window.location = my_target_url(); //...and sends the user to that URL.
}
}
$(document).on('keydown', my_custom_keypress); //when user presses a key, triggers our fxn
사용자를 강제로 연결하는 대신 실제로 대상 링크에서 클릭 이벤트를 트리거하려면 다음과 같이 코드를 약간 수정할 수 있습니다 (다시 말하지만 jQuery가 필요함).
var ENTER = 13; //This is the js keycode for ENTER
function my_custom_keypress(e){
key = e.which;
if(key == ENTER){
$('a#follow-me').trigger('click');
}
}
$(document).on('keydown', my_custom_keypress); //when user presses a key, triggers our fxn
그렇게해야합니다. 행운을 빕니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다