admin管理员组文章数量:1246689
This is a simple code :
HTML
<a id="link" href="#">Ciao</a>
jQuery
$('#link').click(function () {
alert("ciao");
});
in fact, left/middle button is triggered (the alert is printed) but with Right Click? Why not? And how can I trigger it?
This is a simple code :
HTML
<a id="link" href="#">Ciao</a>
jQuery
$('#link').click(function () {
alert("ciao");
});
in fact, left/middle button is triggered (the alert is printed) but with Right Click? Why not? And how can I trigger it?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 9, 2012 at 16:33 markzzzmarkzzz 48k126 gold badges317 silver badges534 bronze badges 1- 1 Related: stackoverflow./questions/1646851/jquery-right-click-event – David Commented Jan 9, 2012 at 16:36
5 Answers
Reset to default 9Bind mousedown
to handle left, middle and right clicks:
$('#link').mousedown(function(e) {
alert("ciao");
});
You can use e.which
to determinate which button has been clicked. 1
: left, 2
: middle and 3
: right.
Here's the demo: http://jsfiddle/fPhDg/9/
You can stop the contextmenu from appearing like this:
$('#link').contextmenu(false);
Demo: http://jsfiddle/y4XDt/
You should use this very very carefully! It only makes sense in some very rare cases.
Use .contextmenu(...)
:
$('#link').contextmenu(function () {
alert("ciao");
});
And if you want to catch both events, you could use the bind(...)
function:
$('#link').bind('click contextmenu',function()
{
alert("ciao");
});
http://jsfiddle/fPhDg/4/
You can subscribe to the click event and the contextmenu event like so:
$('#link').on('click contextmenu', function (e) {
if (e.which === 3) {
// Right mousebutton was clicked
// 1 is left mousebutton
// 2 is centre mousebutton
}
});
try this...
$('#link').mousedown(function (event) {
if(event.which === 1)
alert("left click");
if(event.which === 2)
alert("center click");
if(event.which === 3)
alert("right clikc");
});
fiddle : http://jsfiddle/fPhDg/8/
you can capture right mouse click like this :
<head>
<script>
function whichButton(event)
{
if (event.button==2)
{
alert("You clicked the right mouse button!");
}
else
{
alert("You clicked the left mouse button!");
}
}
</script>
</head>
<body onmousedown="whichButton(event)">
<p>Click in the document. An alert box will alert which mouse button you clicked.</p>
</body>
本文标签: javascriptWhy right click is not triggered with click()Stack Overflow
版权声明:本文标题:javascript - Why right click is not triggered with .click()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740252873a2248826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论