jquery events, registration and recurring event handling

Source: Internet
Author: User

jquery sometimes has a problem of repeatedly registering an event, causing the click of an event to be repeated, that is, the number of times the event was triggered,

Then the event will be repeated several times by the execution overlay.

My side of a project, when a page is initialized, a control to register an event, and later found that the page entered several times, because the entry page will perform a registration, the event will be registered several times, is repeated, no coverage.

Internet query, summed up the following several situations:

1, the event registered by the on method , each time the registration will not overwrite the original method. will be saved as a queue
When clicked, the trigger event will be executed in the order of registration.

 function   Initonevent () { //  Register a click event for the ID onwaytoevent button  $ ("#onWayToEvent")   .on    ("Click", function   () {alert ( "this is one on event "" #onWayToEvent "). On (" click ", function   () {alert ( "This is", "the" on event "" # Onwaytoevent "). On (" click ", function   () {alert (  this was three on event " 

2, the event is registered multiple times via bind method . The method of multiple registration through the Bind method of jquery is the same, it will not overwrite the original method, but also the method to
The queue is saved in the form of the event, and is executed one after the other by the registration order.

function initbindevent () {/// for the Bindwaytoevent-ID button Register Click event $ ("#bindWayToEvent"). Bind ("click",  function() {alert ("This was registry event by Bind. One")}); $ ("#bindWayToEvent"). Bind ("click", function () {alert ("This was registry event by Bind.")}); $ ("#bindWayToEvent"). Bind ("click",  function() {alert ("This was registry event by Bind. Three")}); </div>

3, register the event multiple times through the click Method . The method of registering multiple times through the click Method of jquery is the same as the above effect.

function Initclickvent () {$ ("#clickWayToEvent"). Click (function() {alert ("This was registry event by Click. One ");}); $ ("#clickWayToEvent"). Click (function() {alert ("This was registry event by click.");}); $ ("#clickWayToEvent"). Click (function() {alert ("This was registry event by Click. Three" );});}

4, register events multiple times through the live method .

How do you want to overwrite the previous event, leaving only the last registered event method?

1, unique registration of events via the Unbind,bind method

$ ("#oneEvnetByBind"). Unbind ("click"). Bind ("click",function() {alert ("only!!!!!!!" );});

2, the only load of events through the Die Live method

$ ("#oneEvnetByDieLive"). Die (). Live ("Click",function() {alert ("The One of your!!!!!!" );});

jquery events, registration and recurring event handling

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.