开发者

in jquery, how to make the right click of the mouse have the same behavior of left click

&lta id="link1" href="link1"&gtlink1&lt/a&gt

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:

  1. how can I get the href from jquery in firefox and IE?

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜