Possible Duplicate:
How to distinguish between left and right mouse click with jQuery
How to open context meniu when I click with right mouse click inside the div. Because when I click with right mouse button inside div its fadeouts?
Javascript
$("#link").click(function(e){
e.stopPropagation();
div = $("#mydiv").fadeToggle(300);
});
$("#mydiv").click(function(e){
e.stopPropagation();
});
$(document).click(function(e){
$('#mydiv').fadeOut(200);
});
HTML
<a id="link">Click</a>
<div id="mydiv" style="width: 200px; height: 200px;">Hello</div>
UPDATE:
Javascript
$("#link").click(function(e){
e.stopPropagation();
div = $("#mydiv").fadeToggle(300);
});
$("#good_buttom").click(function(e){
e.stopPropagation();
div = $("#hide").fadeToggle(300);
});
$("#mydiv").mousedown(function(e) {
switch (e.which) {
case 1:
e.stopPropagation();
$('#mydiv').fadeOut(200);
break;
case 2:
alert('2');
break;
case 3:
alert('3');
break;
default:
alert('You have a strange mouse');
}
});
HTML
<a id="link">Click</a>
<div id="mydiv" style="width: 200px; height: 200px;">
<button id="good_button">Click IT</button>
<div id="hide" style="display:none;">Show up.</div>
</div>
clickis only triggered by left mouse click, whilemousedowncan be triggered by left/middle/right mouse click. So just replaceclickwithmousedownin your code, and distinguish left/middel/right click by checkingevent.which. Please check How to distinguish between left and right mouse click with jQuery for an example.BTW: Checking this link may help you understand the relationship between
click,mousedownandmouseupevents. And this article: Javascript Madness: Mouse Events maybe also helps. As a rule of thumb, it’s preferable to handle low-level events likemousedownormouseupinstead of high-level events likeclickordblclickwhen you need more control on mouse events.One more problem is that you put a superfluous “#” before
mydivin HTML.Update: As per your new requirement, you may add the following code to avoid event propagation.