Javascript鼠标事件中的event.button
和event.which
什么区别?
Indicates which mouse button caused the event.
According to jQuery's documentation:
...event.which also normalizes button presses (mousedown and mouseupevents), reporting 1 for left button, 2 for middle, and 3 for right. Use event.which instead of event.button.
In all modern browsers (IE8+) event.button
will give you the following values:
0 Specifies the left mouse-button
1 Specifies the middle mouse-button
2 Specifies the right mouse-button
While in IE8 and earlier:
1 Specifies the left mouse-button
4 Specifies the middle mouse-button
2 Specifies the right mouse-button
event.which
standardize these results by providing you the following values:
1 Specifies the left mouse-button
2 Specifies the middle mouse-button
3 Specifies the right mouse-button
I'm relatively shy to the last solution despite my upvote. According to the MDN documentation :
So, MouseEvent.button is still prefered. An interesting (case) alternative could be MouseEvent.buttons which permits you to determine all buttons pressed when the event is triggered.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.