检测鼠标单击并在新选项卡中打开目标

检测鼠标单击并在新选项卡中打开目标

本文介绍了jQuery:检测鼠标单击并在新选项卡中打开目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

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.

我的解决方案是检测单击鼠标中键和单击鼠标左键的时间,然后执行不同的操作.单击鼠标左键时,我想在窗口中用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.

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

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?

谢谢!

推荐答案

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

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>

这篇关于jQuery:检测鼠标单击并在新选项卡中打开目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:11