添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Bind an event handler to the "click" event, or trigger that event on an element.

Contents:

.on( "click" [, eventData ], handler ) .trigger( "click" ) .on( "click" [, eventData ], handler ) Returns: jQuery

Description: Bind an event handler to the "click" event.

version added: 1.7 .on( "click" [, eventData ], handler )
"click"
Type: string
The string "click" .
eventData
Type: Anything
An object containing data that will be passed to the event handler.
handler
Type: Function ( Event eventObject )
A function to execute each time the event is triggered.

This page describes the click event. For the deprecated .click() method, see .click() .

The click event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event. For example, consider the HTML:

After this code executes, clicking on Trigger the handler will also alert the message.

The click event is only triggered after this exact series of events:

  • The mouse button is depressed while the pointer is inside the element.
  • The mouse button is released while the pointer is inside the element.
  • This is usually the desired sequence before taking an action. If this is not required, the mousedown or mouseup event may be more suitable.

    Examples:

    Hide paragraphs on a page when they are clicked:

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>on demo</title>
    <style>
    p {
    color: red;
    margin: 5px;
    cursor: pointer;
    }
    p:hover {
    background: yellow;
    }
    </style>
    <script src="https://code.jquery.com/jquery-3.7.0.js"></script>
    </head>
    <body>
    <p>First Paragraph</p>
    <p>Second Paragraph</p>
    <p>Yet one more Paragraph</p>
    <script>
    $( "p" ).on( "click", function() {
    $( this ).slideUp();
    } );
    </script>
    </body>
    </html>

    Web hosting by Digital Ocean | CDN by Fastly | Powered by WordPress