Example of trigger () method usage in jQuery, jquerytrigger
This document describes how to use the trigger () method in jQuery. Share it with you for your reference. The specific analysis is as follows:
This method triggers events of the specified type of matching element.
Syntax structure 1:
Specifies the event type triggered by the matching element.
Copy codeThe Code is as follows: $ (selector). trigger (event, param1, param2 ,...)
Parameter List:
Parameters |
Description |
Event |
Specifies the event to be triggered by the specified element. It can be a Custom Event (attached using the bind () function) or any standard event. |
Param |
Optional. Additional parameters passed to the event handler. Additional parameters are particularly useful for custom events. |
Instance code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> trigger () function-helper's house </title>
<Style type = "text/css">
Div {
Width: 200px;
Height: 200px;
Border: 1px solid blue;
}
</Style>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Div"). click (function (){
$ ("Div"). append ("added content ");
});
$ ("Button"). click (function (){
$ ("Div"). trigger ("click ");
})
})
</Script>
</Head>
<Body>
<Div> </div>
<Button> click activation event </button>
</Body>
</Html>
When you click the button, you can start the click event on the div and then execute the click event processing function.
Syntax structure 2:
The event object is used as a parameter to specify the event type to be triggered by the matching element.
Copy codeThe Code is as follows: $ (selector). trigger (eventObj)
Parameter List:
Parameters |
Description |
EventObj |
The event object specifies the function that runs when an event occurs. |
Instance code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> trigger () function-helper's house </title>
<Style type = "text/css">
Div {
Width: 200px;
Height: 200px;
Border: 1px solid blue;
}
</Style>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Div"). click (function (){
$ ("Div"). append ("added content ");
});
Var e = jQuery. Event ("click ");
$ ("Button"). click (function (){
$ ("Div"). trigger (e );
})
})
</Script>
</Head>
<Body>
<Div> </div>
<Button> click activation event </button>
</Body>
</Html>
I hope this article will help you with jQuery programming.