In-depth understanding of the jquery event binding mechanism

Source: Internet
Author: User
Tags naming convention

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

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.