最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jQueryJavaScript - Allow CTRL + Click to open link in new tab - Stack Overflow

programmeradmin0浏览0评论

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 <a href="">example</a> element?

Here is an example of what I am talking about:-

jQuery('#some-link').bind('click', function() {
   window.location = '';
});

If you hold CTRL and click the 'some-link' element, it just opens the page within the current page when using the above code, any ideas on how do this?

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 <a href="">example</a> element?

Here is an example of what I am talking about:-

jQuery('#some-link').bind('click', function() {
   window.location = 'http://someurl.com';
});

If you hold CTRL and click the 'some-link' element, it just opens the page within the current page when using the above code, any ideas on how do this?

Share Improve this question asked Nov 9, 2014 at 1:09 nsilvansilva 5,61217 gold badges73 silver badges112 bronze badges 3
  • Have a look .. stackoverflow.com/questions/4907843/… – saruftw Commented Nov 9, 2014 at 1:12
  • You will probably also want to check for a ctrl key press I think – DannyS3 Commented Nov 9, 2014 at 1:12
  • There are many keys that change the default behavior. You're better off using something like filter-altered-clicks to handle it for you. – fregante Commented Oct 10, 2016 at 17:00
Add a comment  | 

3 Answers 3

Reset to default 24

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

In case anyone wants to disable page navigation on regular click, to make an ajax call or something, but still wants to keep the default ctrl+click and right-click and middle-click(mouse scroll button) , that is the way:

$('#link').bind('click', function(e) {
  if (!e.ctrlKey && e.which != 2 && e.which != 3) {
    e.preventDefault();
    // some logic:
    $('#box').append('<br>').append($('<a>').html('not paging but doing something! Now try with CTRL'));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box">
  <a id="link" href="http://stackoverflow.com/questions/26823884/">this question</a>
</div>

JQuery documentation says:

event.which also normalizes button presses (mousedown and mouseupevents), reporting 1 for left button, 2 for middle, and 3 for right. Use event.which instead of event.button.

On Macintosh keyboards, this is the ⌘ Command key. On Windows keyboards, this is the Windows key (⊞ Windows).

For more see this page:

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey

发布评论

评论列表(0)

  1. 暂无评论