开发者

How to create a different key-command sets for individual JQuery tabs?

开发者 https://www.devze.com 2023-04-04 13:00 出处:网络
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) whe

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'); 
         }
   });
0

精彩评论

暂无评论...
验证码 换一张
取 消