问题描述
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')
}
});
参见
这篇关于jQuery / JavaScript - 允许CTRL +单击以在新选项卡中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!