jQuery/JavaScript - Allow CTRL + Click to open link in new tab

前端 未结 3 2070
悲哀的现实
悲哀的现实 2021-02-14 02:12

Is there a way in jQuery or JavaScript to enable the user to CTRL + Click a link to open a link in a new tab like example element?

3条回答
  •  悲哀的现实
    2021-02-14 03:12

    Check to see if the Ctrl key is pressed, and open the link using window.open. The link may not open in a new tab though. See the discussion here.

    jQuery('#some-link').bind('click', function(e) {
       e.preventDefault(); 
       if (e.ctrlKey){
         window.open('http://someurl.com','_blank')
       }
    });
    

    See JSFiddle

提交回复
热议问题