右键点击继续在Firefox中传播 [英] Right click keeps propagating in Firefox
问题描述
-
我想单击一个链接时显示一个div
-
当我点击文档中的其他位置时,我希望div消失 我不想它会消失,当我点击的div本身
类似这样的:
JS:
(function(){
var box = $('#box' );
$(document).on('click',function(){
if(box.css('display')=='block'){
box.css '''''');
}
});
$('#start')。on('click',function(e){
box。 css({
'text':'Box',
'position':'absolute',
'top':'50px',
'left':'0' ,
'backgrou nd':'#EEE',
'border':'1px solid#555',
'width':'200px',
'height':'50px',
'display':'block'
});
e.stopPropagation();
});
$ b box.on('click',function(e){
e.stopPropagation();
});
))();
这个小提琴工作得很好,但当我测试Firefox(15.0.1),如果你右键单击div,它消失,这不是我正在寻找的行为。看来,stopPropagation()适用于点击,而不是在Firefox中右键单击。 Chrome保持右键单击传播到文档。
我该如何解决这个问题?
谢谢 使用event.which方法来检测哪个按钮被点击。以下是 jsfiddle 中的示例。
if(event.which == 1&& box.css('display')=='块'){box.css('display','none');
}
});
I just noticed something unusual. This is what I want to accomplish:
I want a div to be shown when I click a link
I want the div to disappear when I click somewhere else in the document
I don't want it to disappear when I click the div itself
Something like this:
JS:
(function() {
var box = $('#box');
$(document).on('click', function() {
if (box.css('display') == 'block') {
box.css('display', 'none');
}
});
$('#start').on('click', function(e) {
box.css({
'text': 'Box',
'position': 'absolute',
'top': '50px',
'left': '0',
'background': '#EEE',
'border': '1px solid #555',
'width': '200px',
'height': '50px',
'display': 'block'
});
e.stopPropagation();
});
box.on('click', function(e) {
e.stopPropagation();
});
})();
That fiddle works just fine but when I tested that in Firefox (15.0.1), if you right-click on the div, it dissapears, which is not the behavior I'm looking for. It seems that stopPropagation() works for clicks but not right-clicks in Firefox. Chrome keeps right-clicks from propagating to the document.
How can I fix it?
Thanks
Use the event.which method to detect which button was clicked. Here's an example in jsfiddle.
$(document).on('click', function(event) {
if (event.which == 1 && box.css('display') == 'block') {
box.css('display', 'none');
}
});
这篇关于右键点击继续在Firefox中传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!