in jquery, how to make the right click of the mouse have the same behavior of left click
<a id="link1" href="link1">link1</a>
what i want to realize: when i right c开发者_如何学编程lick or left click these link, i both go to the linked pages. I have several questions:
how can I get the href from jquery in firefox and IE?
how can I identify the mouse event in firefox and IE, the event.which has different behaivors in firefox and in IE
Thank you very much
To get the href
, which is an attribute of the a
tag, you can use
var the_href = $('#link1').attr('href');
This works on all major browsers.
$('#element').mousedown(function(event) {
switch (event.which) {
case 1:
alert('Left mouse button pressed');
break;
case 2:
alert('Middle mouse button pressed');
break;
case 3:
alert('Right mouse button pressed');
$(this).click();
event.preventDefault;
break;
}
});
(borrowed heavily from: How to distinguish between left and right mouse click with jQuery) I'm Not sure about the meaning of your #1
Adding @Camilo Díaz's answer to mine so you can have one complete answer to both your questions:
for #2: var the_href = $('#link1').attr('href')
My stated answer will accomplish your goal without needing the href attribute though.
精彩评论