jQuery: detecting cmd+click / control+click

后端 未结 5 2217
Happy的楠姐
Happy的楠姐 2021-02-18 17:31

i have the options of my web application in tabs.

5条回答
  •  伪装坚强ぢ
    2021-02-18 18:04

    Unfortunately, event.metaKey does not evaluate to true on Windows when the ctrl key is held on click.

    Fortunately, event.ctrlKey does evaluate to true in these situations. Also, you may want to consider shift + clicks in your event handler.

    Thus, your cross platform jquery flavored javascript code would look something like:

    $('ul#tabs li a').on('click', function(e) {
        var link = $(this).attr('href');
    
        // Check "open in new window/tab" key modifiers
        if (e.shiftKey || e.ctrlKey || e.metaKey) {
          window.open(link, '_blank');
        } else {
          $('#content').fadeOut('fast',function(){
            window.location = link;
          });
        }
      }
    });
    

提交回复
热议问题