Detect left mouse button press

后端 未结 5 1785
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 06:44

I hate this mess with the mouse buttons created by W3C an MS! I want to know if the left mouse button is pressed when I get a mousedown event.

I use this code

5条回答
  •  青春惊慌失措
    2020-11-29 07:07

    Updated answer. The following will detect if the left and only the left mouse button is pressed:

    function detectLeftButton(evt) {
        evt = evt || window.event;
        if ("buttons" in evt) {
            return evt.buttons == 1;
        }
        var button = evt.which || evt.button;
        return button == 1;
    }
    

    For much more information about handling mouse events in JavaScript, try http://unixpapa.com/js/mouse.html

提交回复
热议问题