gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:37:57 25 4
gpt4 key购买 nike

是否可以检查链接是否被单击以在新窗口中打开?我的意思是 ctrl 或 shift 键按下是明确的,您可以处理此事件,但是浏览器中的右键单击上下文菜单怎么样?您可以在此处单击在另一个窗口中打开的上下文菜单项..

打开上下文菜单时触发 Jquery context() 方法:

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

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

最佳答案

我想出了这个解决方案,它非常 hacky 和 ​​DIY,但应该可以解决问题。

诀窍是使用此 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

这不会在 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>

关于javascript:如何检查是否在上下文菜单新选项卡上单击了链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50983099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com