简体   繁体   English

javascript:如何检查是否在上下文菜单的新选项卡上单击了链接

[英]javascript: How to check if a link is clicked over context menu new tab

Is it posible to check if a link is clicked to open up in a new window? 是否可以检查是否单击了链接以在新窗口中打开? I mean ctrl or shift key pressing is clear, you can handle this events, but what about the right click context menu in the browser? 我的意思是ctrl或Shift键按下是清楚的,您可以处理此事件,但是浏览器中的右键单击上下文菜单呢? Here you can click the context menu item open in another window.. 在这里,您可以单击在另一个窗口中打开的上下文菜单项。

Jquery context() method triggers when the context menu is opened: 当上下文菜单打开时,jQuery context()方法触发:

$("a").contextmenu(function() {
  alert("context");
});

$("a").click(
    if (evnt.ctrlKey || evnt.shiftKey || evnt.metaKey || 
        (evnt.button && evnt.button == 1)){
        alert("link clicked");
    }
);

I came up with this solution, it's very hacky and DIY but should do the trick. 我想出了这个解决方案,它非常hacky和DIY,但应该可以解决。

The trick is to create your own context menu using this jQuery plugin: 诀窍是使用此jQuery插件创建自己的上下文菜单:

https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.contextMenu.js https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.contextMenu.css https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.ui.position.js https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.contextMenu.js https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.contextMenu.css https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.ui.position.js

This doesn't execute on SO and I recommend running it on JSFiddle: JSFiddle 这不会在SO执行,我建议在JSFiddle上运行它: JSFiddle


 $(function() { $.contextMenu({ selector: '.context-menu-one', callback: function(key, options) { var m = "clicked: " + key; window.console && console.log(m); switch (key) { case "open_new_window": console.log($(this)); //var win = window.open($(this)[0].href, '_blank'); window.open($(this)[0].href,'_blank'); //win.focus(); break; case "cut": break; } }, items: { "open_new_window": { name: "Open link in new tab", icon: "edit" }, "cut": { name: "Cut", icon: "cut" } } }); }); 
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.ui.position.js"></script> <link href="https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.contextMenu.css" rel="stylesheet"/> <script src="https://rawgit.com/swisnl/jQuery-contextMenu/master/dist/jquery.contextMenu.js"></script> <a class="context-menu-one" href="https://www.google.com">Google</a><br> <a class="context-menu-one" href="https://www.bing.com">Bing</a><br> <a class="context-menu-one" href="https://www.yahoo.com">Yahoo</a><br> <a class="context-menu-one" href="https://www.facebook.com">Facebook</a><br> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查是否已单击菜单链接或选项卡,并且用户是否在使用Javascript的链接/页面上? - How to check if a menu link or tab had been clicked on and the user is on the link/page with Javascript? 是否可以使用javascript捕获“在新标签中打开”单击上下文菜单的事件? - Is it possible to capture “Open in New Tab” clicked event of context menu using javascript? 如何打开在新标签页中点击的链接? - How to open the link clicked in a new tab? 检查是否在JavaScript中单击了链接 - Check if link is clicked in JavaScript 如何在新选项卡中打开在 iframe 中的 PDF 文档中单击的链接? - How to open in a new tab a link clicked in a PDF document that is in an iframe? 如何在单击链接时打开新选项卡或窗口? - How can I open a new tab or window when a link is clicked? 如何使用javascript在新标签页中打开链接 - How to open a link in new tab using javascript 如何使用表中的上下文菜单打开新选项卡 - how to open a new tab using context menu in a table 使用 javascript 单击网格行时如何覆盖新选项卡 - How to Override new tab when grid row is clicked using javascript 运行 javascript 的链接打开另一个页面。 如果用户使用中键单击或右键单击菜单,如何在新选项卡中执行此操作 - link running a javascript that open another page. how to make it do it in new tab if user uses middle click or right click menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM