Can I handle a right click event on an HTML <button /> element?

前端 未结 4 1761
孤街浪徒
孤街浪徒 2020-12-31 11:11

I\'ve seen plenty of questions and answers on SO and elsewhere that talk about right click events and how to catch and handle them with JavaScript, generally using the

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 11:44

    Sure, just add a onmousedown listener, check which mouse was pressed:

    JS:

    document.getElementById("test").onmousedown = function(event) {
        if (event.which == 3) {
            alert("right clicked!");
        }
    }
    

    HTML:

    
    

    Demo: http://jsfiddle.net/Jmmqz/

提交回复
热议问题