jQuery: detecting cmd+click / control+click

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

i have the options of my web application in tabs.

5条回答
  •  一整个雨季
    2021-02-18 18:11

    Using e.metaKey doesn't works the same on windows, so to detect for Windows you can use the navigator object and see if the user is clicking the ctrl key (the default way to open a new tab).

    $('ul#tabs li a').click(function(a){
      var href = $(this).attr('href');
      // check if user clicked with command key (for mac) or ctrl key (for windows)
      if(a.metaKey || (navigator.platform.toUpperCase().indexOf('WIN')!==-1 && a.ctrlKey)) {
        window.open(href,'_blank');
      } else {
        $('#content').fadeOut('fast', function(){
            window.location = href;
        });
      }
    });
    

提交回复
热议问题