How to create different key-command sets for use with different JQuery tabs.
I would like the keyboard keys 1 -> 8 to be able to trigger click events for 8 individual buttons located on $(#tab-1) when $(#tab-1) is selected.
I would also like the same keyboard keys 1 -> 8 to be able to trigger click events for a different set of buttons located 开发者_StackOverflowon $(#tab-2) when $(#tab-2) is selected.
I am thinking I may need to toggle between several switch case functions based on whichever tab is selected? Am I on the right track here? Any ideas most appreciated.
use a global variable
var tab_id = 'tab-1';
when click on each tab change global variable
$('.tab').click(function(){
tab_id = $(this).attr('id');
});
now when click 1-8 you can select the buttons in each tab like this ....
$('body').keypress(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 49) {
$("#"+tab_id+" > #button_1").trigger('click');
}
else if(code == 50) {
$("#"+tab_id+" > #button_2").trigger('click');
}
});
精彩评论