AddEventListener and Attachevent

Source: Internet
Author: User

First: A simple universal approach (IE && FF)

Window.onload = function () {
var odiv = document.getElementById ("J_mydiv"); Find Object
Odiv.onclick = function () { //Set event listener functions
Alert ("click");
}
}

Cons: 1. Adding a single event

2. Cannot delete event

Second: The listener function in IE

var odiv;

function Fnclick () {
Alert ("click Me");
Odiv.detachevent ("onclick", fnclick);//delete listener function
}

Window.onload = function () {
Odiv = document.getElementById ("J_mydiv");//Find Object
Odiv.attachevent ("onclick", Fnclick); Adding a listener function
}

Third: Standard DOM listener function

var odiv;

function FnClick1 () {
Alert ("Click1");
Odiv.removeeventlistener ("click", Fnclick2,false); Deleting a listener function 2
}

function FnClick2 () {
Alert ("Click2");
}

Window.onload = function () {

Odiv = document.getElementById ("J_mydiv"); Find Object
Odiv.addeventlistener ("click", Fnclick1,false); Adding a listener function 1
Odiv.addeventlistener ("click", Fnclick2,false); Adding a listener function 2

}


Four: From the JS instance view event monitoring see: http://imethan.com/?p=208

Five: From the example of JS event Monitoring Learning Notes (event monitoring bindings, ff/ie different processing mechanisms for compatibility summary) See: http://hi.baidu.com/dou917/blog/item/40219f37e6e3deee1b4cff67.html

Sixth: Code Summary

<script language= "JavaScript" >
Javascript Event Demo
Window.onload = function () {
var hidebox = function (event) {
document.getElementById (' Status_show '). style.display = ' None ';
document.getElementById (' Status_hide '). style.display = ' block ';
};
var Showbox = function (event) {
document.getElementById (' Status_show '). style.display = ' block ';
document.getElementById (' Status_hide '). style.display = ' None ';
Stopevent (event);
};
var stopevent = function (event) {
E = Event | | window.event;
if (e.stoppropagation) {
E.stoppropagation ();
}else {
E.cancelbubble = true;
}
};
if (Document.addeventlistener) {
Document.addeventlistener (' Click ', Hidebox, false);
document.getElementById (' Status_hide '). AddEventListener (' Click ', Showbox, false);
document.getElementById (' Status_show '). AddEventListener (' Click ', Stopevent, false);
}else {
For IE
Document.attachevent (' onclick ', hidebox);
document.getElementById (' Status_hide '). attachevent (' onclick ', showbox);
document.getElementById (' Status_show '). attachevent (' onclick ', stopevent, Showbox);
}
};
</script>
    • The onclick notation is a DOM0-level specification, which is really supported by all browsers, but there are some drawbacks to this notation:

    1. This notation cannot bind multiple events at the same time

    2. This is how the code is coupled together

    • AddEventListener () is a method defined in the DOM2 standard that can control whether an event handler is invoked during the event capture phase or during the bubbling phase (determined by the third parameter of the function True/false), since this is defined in the DOM2 standard, This method is supported only by browsers that support DOM2-level event handlers (both Ie9,firefox,safari,chrome and opera support this).

    • The Attachevent () method is not defined by the DOM standard, but is implemented by IE itself, and since IE8 and previous versions only support event bubbling, it is conceivable that the event handlers added by this method will only be called during the bubbling Phase, AddEventListener () and attachevent () Another difference is the first parameter-the event type, attachevent () event type is preceded by ' on ', such as click event Click, AddEventListener () can pass "click", and Attachevent () but to pass the ' onclick ';

AddEventListener and Attachevent

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.