For cases where only one trigger is required and then immediately unbound,jQuery provides a wayfor one(), which has the same syntax structure as bind .
Trigger ("click") can be simplified into click (). Trigger can also trigger events with custom names. For example, bind a custom Myclick event to an element with bind ("Myclick", function () {}) , and then use the trigger ( "Myclick") to trigger the event.
$ ("button"). One ("click", Function () {
Console.log ("one");
$ ("input"). Trigger ("focus");
the onfus event, which is bound by input , will trigger the default action of the browser and move the focus to input .
$ ("input"). Triggerhandler ("Focus");
Only the Onfus event bound by input is triggered , and the browser default action is not performed.
})
$ ("input"). focus (function () {
Console.log ("input");
})
$ ("div"). Bind ("mouseover mouseout", function () {
Binding Multiple Events
$ (this). Toggleclass ("color");
})
add a namespace after the bound event name for unified management
$ (' div '). bind ("Click.plugin", function () {
Console.log (' Click has a name! ');
})
$ (' div '). bind ("Mouseover.plugin", function () {
Console.log (' mouseover div ');
})
$ (' div '). bind ("click", Function () {
Console.log (' Click Have not a name! ');
})
$ ("button"). Click (function () {
$ ("div"). Unbind (". Plugin");
})
$ ("button"). EQ (1). Click (function () {
$ ("div"). Trigger ("click!");
The exclamation point means matching all click events that are not under the naming convention
})
In-depth understanding of the jquery event binding mechanism