开发者

jQuery use escape key to close toggle

开发者 https://www.devze.com 2022-12-29 15:28 出处:网络
2 part question her开发者_运维百科e...I have several DIVs that use a simple .click show() function to display content.Is there a way i can combine these to trim my code?I wasnt sure, because they all

2 part question her开发者_运维百科e...I have several DIVs that use a simple .click show() function to display content. Is there a way i can combine these to trim my code? I wasnt sure, because they all are displaying DIV with Unique ID's.

$('a#hmo-blue-lnk1').click(function() {
    $('#hmo-blue1').show();
    return false;
  });

$('a#hmo-blue-lnk2').click(function() {
    $('#hmo-blue2').show();
    return false;
  });

$('a#hmo-blue-lnk3').click(function() {
    $('#hmo-blue3').show();
    return false;
  });

i close them via this snippet:

$('a.close').click(function() { 
     $('#hmo-blue1').hide();
     $('#hmo-blue2').hide();
     $('#hmo-blue3').hide();

     return false; 
  });

2nd part of the question... how can i integrate the aspect that the user can just hit the escape key to also hice (or close) the box that is shown?


For the first part:

    for (i=1; i<=3; i++)
    {
           $('a#hmo-blue-lnk'+i).click(function() {
                 $('#hmo-blue'+i).show();
                 return false;
           });
    }

For the second part.

$(document).keypress(function(e) { 
    if (e.which == 27) {
          for (i=i; i <=3; i++)
          {
                 $('#hmo-blue'+i).hide();
          }
    }
});

You could change your hide function to a for loop too, then call it instead of closing too by doing $('a.close').click() which would call a click on a.close instead.

0

精彩评论

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