JavaScript event mechanism compatible with "detailed collation" _ Basics

Source: Internet
Author: User

"Add event mechanism" AddEventListener and attachevent

[The consortium]

AddEventListener (' click ', function () {alert (' Hello World ')}, FALSE)//W3C specification add event (IE8 and above incompatible); The first parameter is the event type, the second is the event program, and the third F Alse for event bubbling, true for event capture

[IE]

Attachevent (' onclick ', function () {alert (' Hello World ')}//ie add event; The first parameter is the event type (needs to be added on); The second is the event program, because IE only supports event bubbling, so there are only two parameters

[Cross-browser compatible]

function Insertevent (obj,event,fun) {

if (obj.addevenlistener) {

 Addeventlitener (event,fun,false)

} else if (Obj.attach) {

attachevent (' on ' +event,fun)

}

}

"Delete event mechanism" RemoveEventListener detachevent

[The consortium] RemoveEventListener

RemoveEventListener ()///Use AddEventListener new events can only be deleted by RemoveEventListener;

[note]//The following way of deleting events is incorrect because the event program must not be an anonymous function

AddEventListener (' click ', function () {alert (' Hello World ')}, False)

RemoveEventListener (' click ', function () {alert (' Hello World ')},false)

Solution

AddEventListener (' Click ', box, false);

RemoveEventListener (' click ', Box,false);

function box () {

alert (' Hello world ');

}

[Cross-browser compatible]

function Deleteevent (obj,event,fun) {

if (obj.removeeventlistener) {

RemoveEventListener (event,fun,false  );

}else if (obj.detachevent) {

detachevent (' on ' +event,fun)

}

}

"Block default behavior for specific events"

[Preventdefault and ReturnValue]

[The consortium] Preventdefault

[IE] Reutrnvalue

[Cross-browser compatible]

function (event) {

Event=event | | window.event;

if (event.preventdefault) {

event.preventdefault ()

}else{

event.returnvalue=false;

}

}

"Get target Object"

[Target and Srcelement]

function (event) {

Event=event | | window.event;

if (event.target) {return

event.target;

}else if (event.srcelement) {return

event.srcelement;

}

 

}

The above JavaScript event mechanism compatible with "detailed collation" is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.