jquery 部分屏蔽浏览器右击菜单问题

wkywj1 2015-02-03 02:55:51
RT,
$('#div1').bind('contextmenu',function(){
return false;
});


对div1进行了屏蔽(这时可以正常屏蔽),但后面对div1中的 div 绑定了鼠标右击事件(右击div 出现自己写的菜单),这时自己写的菜单和浏览器的右击菜单都会出现。。
请问大神们,这个该怎么解决!
...全文
1927 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
窗帘 2017-07-05
  • 打赏
  • 举报
回复
遇到了 刚解决 鼠标右击弹出的那块的jsp代码应该在你阻止右击事件的那个控件的里面 jsp代码部分
Taolandd 2017-05-12
  • 打赏
  • 举报
回复
引用 25 楼 qq_38371402 的回复:
我也遇到了同样的问题,上面的貌似都没用,我的元素还是是动态添加的,不知楼主解决了吗,跪求解决方法,谢谢!
可以啊,动态生成的需要用on绑定到document上: //屏蔽原生右键 $(document).on('contextmenu','#abc',function(){ return false; }); //自己写的右键菜单 $(document).on('mousedown','#abc',function(e){ if(e.which=="3"){ .... } });
qq_38371402 2017-05-07
  • 打赏
  • 举报
回复
我也遇到了同样的问题,上面的貌似都没用,我的元素还是是动态添加的,不知楼主解决了吗,跪求解决方法,谢谢!
帕奇式 2017-04-06
  • 打赏
  • 举报
回复
$('#div1').bind('contextmenu',function(e){ e.stopPropagation(); e.preventDefault(); });
  • 打赏
  • 举报
回复
遇到同样的问题,但楼上的回答,均不能解决。。。。 不知楼主最后如何解决了?
  • 打赏
  • 举报
回复
不同的浏览器的执行效果是不一样的,关键是要阻止代码默认时间的发生 event.preventDefault ?event.preventDefault():event.returnValue =false;
冰羽幻灵 2016-08-24
  • 打赏
  • 举报
回复
补充一句,要阻止事件冒泡才行
冰羽幻灵 2016-08-24
  • 打赏
  • 举报
回复
刚刚遇到同样的问题,现在解决了,分享一下: $('#div1').mousedown(function(e){ if(e.which == 3){ e && e.preventDefault ? e.preventDefault()/*W3C*/: window.event.returnValue = false/*IE*/; } })
冰羽幻灵 2016-08-24
  • 打赏
  • 举报
回复
引用 18 楼 jslang 的回复:
[quote=引用 17 楼 shen332401890 的回复:]

<html>
<head>	
	<SCRIPT src="./jquery-1.11.1.js" type="text/javascript"></SCRIPT>
	
</head>
<body>
	<div id="aaa" style="height:1000px;widht:1000px;background:red">aaa
		<div id="bbb" style="height:500px;width:500px;background:green">bbb</div>
	</div>
</body>
	<script>
		$(document).ready(function() {
			
			
			$("#aaa").bind('contextmenu',function(){
				return false;
			});
			$("#bbb").mousedown(function(){
				alert("bbb");
				return false;
			  });
			/*
			
			*/
		});
		 
		

	</script>
</html>
我这样也可以啊 版本 40.0.2214.111 m
测试完全正常[/quote] 这位兄台,你用alert的话,不禁用右键也可以的,既然弹出了弹窗怎么可能还会再弹菜单呢?
wkywj1 2015-02-07
  • 打赏
  • 举报
回复
引用 14 楼 jslang 的回复:
不可能啊,用的什么浏览器?
chrome啊。。。
天际的海浪 2015-02-07
  • 打赏
  • 举报
回复
引用 17 楼 shen332401890 的回复:

<html>
<head>	
	<SCRIPT src="./jquery-1.11.1.js" type="text/javascript"></SCRIPT>
	
</head>
<body>
	<div id="aaa" style="height:1000px;widht:1000px;background:red">aaa
		<div id="bbb" style="height:500px;width:500px;background:green">bbb</div>
	</div>
</body>
	<script>
		$(document).ready(function() {
			
			
			$("#aaa").bind('contextmenu',function(){
				return false;
			});
			$("#bbb").mousedown(function(){
				alert("bbb");
				return false;
			  });
			/*
			
			*/
		});
		 
		

	</script>
</html>
我这样也可以啊 版本 40.0.2214.111 m
测试完全正常
顾小林 2015-02-07
  • 打赏
  • 举报
回复

<html>
<head>	
	<SCRIPT src="./jquery-1.11.1.js" type="text/javascript"></SCRIPT>
	
</head>
<body>
	<div id="aaa" style="height:1000px;widht:1000px;background:red">aaa
		<div id="bbb" style="height:500px;width:500px;background:green">bbb</div>
	</div>
</body>
	<script>
		$(document).ready(function() {
			
			
			$("#aaa").bind('contextmenu',function(){
				return false;
			});
			$("#bbb").mousedown(function(){
				alert("bbb");
				return false;
			  });
			/*
			
			*/
		});
		 
		

	</script>
</html>
我这样也可以啊 版本 40.0.2214.111 m
顾小林 2015-02-07
  • 打赏
  • 举报
回复

<html>
<head>	
	<SCRIPT src="./jquery-1.11.1.js" type="text/javascript"></SCRIPT>
	
</head>
<body>
	<div id="aaa" style="height:1000px;widht:1000px;background:red">aaa
		<div id="bbb" style="height:500px;width:500px;background:green">bbb</div>
	</div>
</body>
	<script>
		$(document).ready(function() {
			
			
			$(document).bind('contextmenu',function(){
				return false;
			});
			$("#bbb").mousedown(function(){
				alert("bbb");
				return false;
			  });
			/*
			
			*/
		});
		 
		

	</script>
</html>
wkywj1 2015-02-06
  • 打赏
  • 举报
回复
引用 11 楼 jslang 的回复:
一定要用mousedown事件的话可以这样

$('.file_list').mousedown(function(e){
//代码。。。。。
}).bind('contextmenu',function(e){
	e.preventDefault();
	return false;
});

试过了也没用。。
天际的海浪 2015-02-06
  • 打赏
  • 举报
回复
不可能啊,用的什么浏览器?
wkywj1 2015-02-05
  • 打赏
  • 举报
回复
引用 4 楼 jslang 的回复:
[quote=引用 2 楼 wkywj1 的回复:] [quote=引用 1 楼 jslang 的回复:] 你在弹出自己做的菜单的事件也让他return false;或者event.preventDefault()
试过了,没用啊。。[/quote] 代码发出来看看[/quote]见楼上
wkywj1 2015-02-05
  • 打赏
  • 举报
回复
引用 5 楼 xcmcc 的回复:
代码发出来看看
见楼上
wkywj1 2015-02-05
  • 打赏
  • 举报
回复
引用 3 楼 functionsub 的回复:
没办法,return false只是阻止浏览器默认事件。你写的右键菜单本身是执行自己的方法,并且阻止了浏览器默认事件。 只能设置一个变量,在右键的时候判断是不是要弹出右键菜单。 每次更新这个变量就可以了。

$('.file_list').mousedown(function(e){
			var path = $(this).attr('path');
			var name = $(this).attr('name');
			var file_id = $(this).attr('file-id');
			//var file_info = {'path':path, 'name':name, 'file_id':file_id};
			//localStorage.setItem('file_info', file_info);
			localStorage.setItem('path',path);
			localStorage.setItem('name',name);
			localStorage.setItem('file_id',file_id);
			if(3 == e.which){
				e.preventDefault();
				var offsetLeft = e.pageX;
				var offsetTop = e.pageY;
				var tipStyle = 'top:'+offsetTop+'px;left:'+offsetLeft+'px;';
				$('#file_action').hide();
				$('#folder_action').hide();
				if($('#file_action').css('display') == 'none'){
					$('#file_action').attr('style',tipStyle).show();
				}else{
					$('#file_action').hide();
				}
			}
		});
现在我就是全局屏蔽。。 $(document).bind('contextmenu',function(){ return false; });
天际的海浪 2015-02-05
  • 打赏
  • 举报
回复
一定要用mousedown事件的话可以这样

$('.file_list').mousedown(function(e){
//代码。。。。。
}).bind('contextmenu',function(e){
	e.preventDefault();
	return false;
});

wkywj1 2015-02-05
  • 打赏
  • 举报
回复
引用 9 楼 jslang 的回复:
$('.file_list').mousedown(function(e){ 改用 $('.file_list').bind('contextmenu',function(e){
那我mousedown写哪里?写在bind里还是什么?
加载更多回复(6)

87,908

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧