开发者

How to disable right-click on a hyperlink in html

I need to disable right-click on a hyperlink which is in a span. I need to disable only one link from the whole page. Any suggestio开发者_JS百科ns?


If you dont want to show the context menu on hyperlink, you can do so without doing anything to other part or even span where it exists. I tested in IE, Firefox and it works.

<a href="#" oncontextmenu="return false;"> Link </a>


This should work:

oncontextmenu=”return false;”     

Place it on any element you want to disable right click for.
Be aware that this causes bad user experience and users can disable it very easily.
Disclaimer: not tested.


If you don't want to pollute your HTML with inline events and you care about supporting IE < 9, you can use this lovely mess:

function addEvent (el, eventType, listener) {
    if (el.addEventListener) { // W3C-compliant
        el.addEventListener(eventType, listener, false);
    }
    else {// IE-specific
        el.attachEvent('on'+eventType, listener);
    }
}
addEvent(document.getElementById('myLinkID'), 'contextmenu', function (e) {
    if (e.preventDefault) { // W3C
        e.preventDefault();
    }
    else { // IE
        e.returnValue = false;
    }
});


I have never seen one done through HTML (that does not imply it is not possible). However, JavaScript can help you here.

You can do something like:

var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;


Try this oncontextmenu="return false;"


IN MVC:

@Html.ActionLink("print page", "myprint", "print", null, new { @oncontextmenu="return false;"})


You can also use jQuery:

    $(".myHyperlinks").contextmenu(function () { return false; });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜