Event Bindings 1

Source: Internet
Author: User

There are two types of event bindings, traditional event binding, and modern event binding. Traditional event bindings have inline mode and script mode. The script pattern assigns a function body to an object's event properties.

window.onload=function() {   var domb=document.getelementbyid (' BD ');     Domb.onclick=function() {    alert (' button click ');};
};

Problems with traditional event bindings:

1. Event properties of the same object multiple functions are assigned many times, and the front is overwritten, showing only the last one.

Workaround, save the event for the first function and then execute it again.

window.onload=function() {                 alert (' Q ');                                      };         if (typeof window.onload== ' function ')        {             var saved=null;              Saved=window.onload;           }          Window.onload=function() {                saved ();                 Alert (' s ');                                      };                             


2. Switching of different events under the same event type

Workaround: When the first event is executed, the second event is assigned a click event in the first event function.

Wrap the Add event in a function. and solve the above problems together.

A key-value pair inside an object can also be accessed as an array, with attributes placed in brackets. An event function, which should not put the same name//event function.

function addevent (OBJ,TYPE,FN) {    var saveevent=null;         if (typeof obj[' on ' +type]== ' function ')    saveevent=obj[' on ' +type];    obj[' on ' +type]=function() {        if(saveevent)        saveevent ();        Fn.call (this);}    ;}

Must be removed after adding an event, or memory will overflow

// Delete Event function removeevent (obj,type) {    if(obj[' on ' +type])    obj[' on ' +type]=null;    } 

Problems that exist:

1. How to avoid adding functions that have already been added. You need to traverse the event, with duplicate names not added.

2. Delete an event, the exact deletion of an object, a type, a function of a name. Avoid accidental deletion.

Event Bindings 1

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.