[英]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.