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