在Safari for Mac中使用Ctrl + Click时,如何禁用contextmenu事件中的单击事件? [英] How do you disable click events from the contextmenu event when using Ctrl+Click in Safari for Mac?

查看:242
本文介绍了在Safari for Mac中使用Ctrl + Click时,如何禁用contextmenu事件中的单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ctrl + click在Mac OS 10.9上的Safari中触发上下文菜单事件(Context.JS)时,mousedown / up / click事件也会触发。这会导致菜单关闭。这些事件似乎是彼此异步发生的,所以stopPropagation不起作用,这似乎也会导致间歇性的行为,有时它很好,有时它不是。

When using ctrl+ click to fire a contextmenu event (Context.JS) in Safari on Mac OS 10.9, the mousedown/up/click events also fire. This causes the menu to be closed. The events seem to occur asynchronously in relation to one another, so stopPropagation doesn't work and this also seems to result in intermittent behaviour, sometimes it's fine sometimes it's not.

有没有其他人遇到过这个问题,如果是这样的话,你和他你是如何解决它/解决它的?

Has anyone else come across this problem, if so did you & how did you resolve it / work around it?

不幸的是我不能向大众发布代码,但我希望有人听起来很熟悉在那里。

Unfortunately I'm not in a position to release the code to the masses, but I am hoping it sounds familiar to somebody out there.

小提琴: http://jsfiddle.net/gnh2tuyj/

推荐答案

你可以利用 ctrlKey property of MouseEvent:

You could make use of the ctrlKey property of the MouseEvent :

var div = document.querySelector('div');
div.addEventListener('click', function(e) {
  if (e.ctrlKey) return;
  e.preventDefault();
  alert('click!');
}, false);

div.addEventListener('contextmenu', function(e) {
  e.preventDefault();
  alert('context menu!');
}, false);

div {
  border: 1px solid red;
}

<div>hold ctrl+click in safari, chrome, etc</div>

所以如果要修补 context.js 你自己,只需添加 if(ctrlKey)return; l24。

So if you want to patch the context.js yourself, just add if(ctrlKey) return; l24.

l23    $(document).on('click', 'html', function (e) {
l24    if(e.ctrlKey) return;
l25    $('.dropdown-context').fadeOut(options.fadeSpeed, function(){
l26     $('.dropdown-context').css({display:''}).find('.drop-left').removeClass('drop-left');
l27     });
l28    });

修补脚本: http://pastebin.com/6ySveRty

这篇关于在Safari for Mac中使用Ctrl + Click时,如何禁用contextmenu事件中的单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆