Home > Web Front-end > JS Tutorial > body text

How to distinguish between left mouse button and right mouse click in jquery

coldplay.xixi
Release: 2023-01-04 09:36:52
Original
2380 people have browsed it

Jquery method to distinguish left or right mouse clicks: You can use the [event.which] attribute to distinguish right clicks. The [event.which] attribute returns which keyboard key or mouse button was pressed on the specified event, code It is [switch (event.which)].

How to distinguish between left mouse button and right mouse click in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, thinkpad t480 computer.

Recommended: jquery video tutorial

##How to differentiate between left click and right click in jquery:

You can use the

event.which attribute in jquery to distinguish right clicks. event.whichProperty returns which keyboard key or mouse button was pressed on the specified event.

event.which provides 1, 2 or 3 for the left, middle and right mouse buttons respectively.

$('#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.');
            break;
        default:
            alert('You have a strange Mouse!');
    }});
Copy after login

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to distinguish between left mouse button and right mouse click in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!