JavaScript Enhanced custom Event event _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

$ (). Ready (function () {
for (Var i=0;i<5;i++) {
/**
* This kind of writing is not professional, if in the cycle, will register 5 events, click After Alert5 times
*/
$ ("#aa"). Click (function () {
Alert ("Hahahaha");
// });
/**
* This type of writing is professional, before registering a click event, first unbind the Click event, and then bind a click event, so to the end only bound a click event
*/
$ ("#aa"). Unbind ("click");
$ ("#aa"). Bind ("click", Function () {
Alert ("Oooooo");
});
}
});

Custom events:
Copy Code code as follows:

$ (). Ready (function () {
/**
* Custom event, triggering at click
*/
$ ("#aa"). Bind ("click", Function () {
Event triggers that trigger a custom event
$ (this). Trigger ("point me");
});
Customizing a "point Me" event
$ ("#aa"). Unbind ("point Me");
$ ("#aa"). Bind ("point Me", function () {
Alert ("Point Me");
});
});

To pass a custom event for a parameter:
Copy Code code as follows:

$ (). Ready (function () {
/**
* Custom event, triggering at click
*/
$ ("#aa"). Bind ("click", Function () {
Event triggers, triggering a custom event, passing arguments
$ (this). Trigger ("Point Me", [' John ', ' Dick ']);
});
Customizing a "point Me" event
$ ("#aa"). Unbind ("point Me");
The first parameter is fixed, followed by custom, even if the event is written in another name, it is a fixed type, the mouse event
$ ("#aa"). Bind ("point Me", function (event,a,b) {
Alert ("Point Me");
alert (a);
alert (b);
});
});

Practice:

Write a custom event that binds the event to a Drop-down list box

When the Drop-down list box selects an item, it triggers the event, passing the selected value in the form of a parameter to the custom event and outputting it.

Html:
Copy Code code as follows:

<select id= "Select" >
<option value= ">111</option>"
<option value= "222" >222</option>
<option value= "333" >333</option>
</select>

Js:
Copy Code code as follows:

$ (). Ready (function () {
$ ("option"). Unbind ("click");
$ ("option"). Bind ("click", Function () {
$ (this). Trigger ("Select and display", [$ (this). Val ()]);
});

$ ("option"). Unbind ("Select and display");
$ ("option"). Bind ("Select and display", function (event,value) {
Alert (value);
});

});
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.