Instance code
Copy codeThe Code is as follows:
<Body>
<Div id = "demo"> </div>
<Button id = "btn"> trigger it </button>
<Script type = "text/javascript">
(Function ($ ){
// Demo1
$ ("# Demo"). bind ("demo-trigger", function (e, args ){
Var info = [];
// Obtain the corresponding data from The args Parameter
For (var prop in args ){
Info. push (prop + ":" + args [prop]);
}
This. innerHTML = info. join (';');
});
$ ('# Btn'). click (function (){
// We pass data into the trigger method as a parameter together with the event name we care about
$ ('# Demo'). trigger ('demo-trigger ',{
Name: 'Andrew ',
Age: '23 ',
Job: 'frontend dev'
});
});
/** Demo2
$ ("# Demo"). bind ("demo-trigger", function (e ){
Var info = [];
// We use the passed e. extra to obtain the imported data.
For (var prop in e. extra ){
Info. push (prop + ":" + e. extra [prop]);
}
// Display
This. innerHTML = info. join (';');
});
$ ('# Btn'). click (function (){
// This usage is very interesting. The parameter of a new jQuery Event object is the name of the Event we care about.
Var event = new jQuery. Event ("demo-trigger ");
// Append an attribute to this event to include our data
Event. extra = {
Name: 'Andrew ',
Age: '23 ',
Job: 'frontend dev'
};
// Finally pass the event into the trigger method... view the above $ ('# demo ').....
$ ('# Demo'). trigger (event );
});**/
}) (JQuery );
</Script>
</Body>