Recently encountered Attachevent and AddEventListener two events, yo, decisive study ~ ~
First to popularize the basic knowledge:
Attachevent and AddEventListener: Attach other processing events to an event.
Online is generally said attachevent for non-Mozilla series, AddEventListener for the Mozilla series.
(Mozilla series in the browser, that is, the Firefox browser, I think it can also refer to the series of browsers supporting the standard)
In theory, you have to try it yourself!
The following code was written in conjunction with the lookup material to test:
<! DOCTYPE html>varBtn1obj = document.getElementById ("BTN1"); functionmethod1 () {alert ("Method1"); } functionmethod2 () {alert ("Method2"); } functionmethod3 () {alert ("Method3"); } functionAttacheventlisten (obj,e,fun) {/*obj.attachevent obj.attachevent ("on" +e,fun): Obj.addeventlistener (e,fun,false);*/Obj.attachevent ("On" +e,fun);//Test Browser, found: Firefox, Chrome, IE9, IE10, IE11, Safari, Opera are incompatible, IE7, IE8 compatible, //Obj.addeventlistener (E,fun,false);//test browser, found: Firefox, Chrome, IE, Safari, opera are compatible, IE7, IE8 incompatible,} attacheventlisten (Btn1obj,"Click", METHOD1); Attacheventlisten (Btn1obj,"Click", METHOD2); Attacheventlisten (Btn1obj,"Click", method3);</script>The run result comment is in the code above. (only major browsers were tested: Ie7-11, Firefox, Chrome, Opera, Safari)
To sum up is:
attachevent--compatible: IE7, IE8, not compatible with Firefox, Chrome, IE9, IE10, IE11, Safari, Opera
addeventlistener--compatible: Firefox, Chrome, IE, Safari, Opera; incompatible IE7, IE8
It can also be seen from the beginning of IE9, many JS has been in accordance with the standards of the Web, other mainstream browsers are also in line with the standard.
A true example of the difference between attachevent and AddEventListener