Code for passing dynamic parameters for Event handler in jquery

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.