jQuery / JavaScript - 允许CTRL +单击以在新选项卡中打开链接 [英] jQuery/JavaScript - Allow CTRL + Click to open link in new tab

查看:102
本文介绍了jQuery / JavaScript - 允许CTRL +单击以在新选项卡中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery或JavaScript中是否有一种方法可以让用户按CTRL +点击链接在新标签页中打开链接,例如< a href => example< / a> 元素?

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';
});

如果按住CTRL并单击some-link元素,它只会打开当前页面使用上面的代码,有关如何做的任何想法?

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?

推荐答案

检查键,然后使用 window.open 打开链接。但是,链接可能无法在新选项卡中打开。请参阅此处的讨论。

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')
   }
});

参见 JSFiddle

这篇关于jQuery / JavaScript - 允许CTRL +单击以在新选项卡中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆