jQuery:检测鼠标点击并在新标签中打开目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7554507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:59:49  来源:igfitidea点击:

jQuery: Detect Mouse Click and Open Target in New Tab

jquery

提问by Phillip

I'm currently designing a simple forum application. It's mostly powered by jQuery/AJAX and loads everything on the same page; however, I know that sometimes users want to open several topics at once to look at them in new tabs when browsing a forum.

我目前正在设计一个简单的论坛应用程序。它主要由 jQuery/AJAX 提供支持,并在同一页面上加载所有内容;但是,我知道有时用户在浏览论坛时希望一次打开多个主题以在新选项卡中查看它们。

My solution to this was to detect when the middle mouse button is clicked and the left mouse button is clicked, and doing different actions then. I want to load the target with AJAX in the window when the left-mouse button is clicked, otherwise load it in a new tab.

我对此的解决方案是检测何时单击鼠标中键和单击鼠标左键,然后执行不同的操作。我想在单击鼠标左键时在窗口中使用 AJAX 加载目标,否则将其加载到新选项卡中。

My only problem is I don't know of a way to open a target location in a new tab with jQuery. Obviously opening new tabs at will isn't allowed, but is there a way to assign this type of behavior to a user-generated action?

我唯一的问题是我不知道使用 jQuery 在新选项卡中打开目标位置的方法。显然不允许随意打开新标签,但是有没有办法将这种类型的行为分配给用户生成的操作?

Thanks!

谢谢!

回答by Nimit Dudani

Please take look on sample code. It may help

请查看示例代码。它可能有帮助

<script type='text/javascript'>
    jQuery(function($){
        $('a').mousedown(function(event) {
            switch (event.which) {
                case 1:
                    //alert('Left mouse button pressed');
                    $(this).attr('target','_self');
                    break;
                case 2:
                    //alert('Middle mouse button pressed');
                    $(this).attr('target','_blank');
                    break;
                case 3:
                    //alert('Right mouse button pressed');
                    $(this).attr('target','_blank');
                    break;
                default:
                    //alert('You have a strange mouse');
                    $(this).attr('target','_self"');
            }
        });
    });
</script>

回答by Kanishka Panamaldeniya

<a href="some url" target="_newtab">content of the anchor</a>

In javascript you can use

在javascript中你可以使用

$('#element').mousedown(function(event) {
      if(event.which == 3) { // right click
          window.open('page.html','_newtab');
      }
})

回答by Willster

You need to also consider that ctrl-click and cmd-click are used to open new tabs (in windows/linux and mac respectively. Therefore, this would be a more complete solution:

您还需要考虑使用 ctrl-click 和 cmd-click 打开新选项卡(分别在 windows/linux 和 mac 中。因此,这将是一个更完整的解决方案:

jQuery.isClickEventRequestingNewTab = function(clickEvent) {
    return clickEvent.metaKey || clickEvent.ctrlKey || clickEvent.which === 2;
};

回答by Ehtesham

    $('#element').mousedown(function(event) {
          if(event.which == 3) { // right click
              window.open("newlocation.html","");
          }
    });

See it live http://jsfiddle.net/8CHTm/1/

现场观看http://jsfiddle.net/8CHTm/1/

回答by CoderHulk

The default action of the middle mouse button is to open in a new tab.

鼠标中键的默认操作是在新选项卡中打开。

You could set the hyperlinks target attribute to _blank to open a new tab.

您可以将超链接目标属性设置为 _blank 以打开新选项卡。

<a href="#link" target="_blank">Link</a>

You can also refer to this question How to distinguish between left and right mouse click with jQuery

你也可以参考这个问题How to区分鼠标左键和右键单击与jQuery

回答by Mitul

Please try "_newTab" instead of "_blank"

请尝试“_newTab”而不是“_blank”

window.open( URL , "_newtab");

window.open( URL , "_newtab");