【问题标题】:Capture the event click of "Open Link in New Tab" javascript or jquery - Cloaking URL捕获“在新选项卡中打开链接”的事件点击 javascript 或 jquery - 隐藏 URL
【发布时间】:2023-03-20 10:41:01
【问题描述】:

我知道我可以通过以下方式捕获click: left, right, middle button的事件:

contextmenu    
e.which

但是,我需要捕获“在新标签中打开链接”的事件点击: 当您在链接中单击鼠标右键时,它会显示一个带有“在新选项卡中打开链接”选项的菜单。

如何捕捉该事件?

PD:不是右键事件,是在“在新标签中打开链接”时点击的事件。

主要原因是,我试图隐藏附属网站的链接,但是当我右键单击并选择“在新选项卡中打开链接”选项时,不起作用,它没有显示真正的隐藏网址.

我在 google 中找到了脚本,但我根据需要编辑了脚本,但我遇到了上面所说的问题。

代码:

(function ($) {

    ninja_href(".ninja-href");

function ninja_href_call(e,which)
{

  var ninja_url = e.target.getAttribute('data-ninja-url');
  var ninja_target = e.target.getAttribute('data-ninja-target');

  if(ninja_target == null || typeof ninja_target == undefined || which === 3)
  {
    ninja_target = "_self";
  }

  if(which === 2)
  {
    ninja_target = "_blank";
  }


  var win = window.open(ninja_url, ninja_target);
  if (win && ninja_target == "_blank")
  {
    win.focus();
  }
}



function ninja_href(element)
{
  if(element == null || typeof element == undefined){
    element = ".ninja-href";
  }

  if (document.addEventListener) 
  {
    document.addEventListener('click', function(e) {
      if(e.target && e.target.matches(element))
      {
        if (e.which === 1 || e.which === 2) 
        {
          e.preventDefault();
          ninja_href_call(e,e.which);

        }
      }
    }, false);

    document.addEventListener('mousedown', function(e) {
        if(e.target && e.target.matches(element))
        {
            if (e.which === 2) 
            {
                e.preventDefault();
                ninja_href_call(e,e.which);

            }
        }

    }, false);

    document.addEventListener('contextmenu', function(e) {
        console.warn(e);
      if(e.target && e.target.matches(element))
      {

      }
    }, false);
  } else {
    document.attachEvent('click', function() {
      if(e.target && e.target.matches(element))
      {
        if (e.which === 1 || e.which === 2) 
        {
          e.preventDefault();
          ninja_href_call(e,e.which);

        }
      }
    });

  }

}

}(window.jQuery));

任何想法

【问题讨论】:

  • 你不能,因为它不会发生在你可以控制的页面的任何元素上——这是浏览器 UI 的一部分。
  • 你能解释一下你想检测什么吗?也许有人可以找到其他方法。
  • 我编辑了我的问题
  • 有什么想法吗?

标签: javascript jquery


【解决方案1】:

请看下面...这是您的处理方式。

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

【讨论】:

  • 很好用,中键抓图,秒(左键抓图)怎么样
  • 有什么想法吗?
猜你喜欢
  • 2017-07-27
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多