About the compatibility of attachevent and AddEventListener in native event bindings and the issue of this reference window in attachevent functionsby OdacashAt 2014-07-0129 Read0 replies.0.0 Greek dollarsClick on the " Accept as Answer" button in the answer below and select the contribution (Nozomi) ratio and click on the "Confirm Checkout" button.
About compatibility in attachevent and AddEventListener in native event bindings and the "This" in the Attachevent function in a window problem
You need to use AddEventListener in ATTACHEVENT,FF and Chrome in the native event bindings for IE bindings.
Attachevent (event, function)
For example: var obtn = document.getElementById (' button ');
Obtn.addachevent (' onclick ', function () {
Alert (' a ');
})
Obtn.addachevent (' onclick ', function () {
Alert (' B ');
})
AddEventListener (event, function, False)
For example: var obtn = document.getElementById (' button ');
Obtn.addeventlistener (' click ', function () {
Alert (' a ');
},false)
Obtn.addeventlistener (' click ', function () {
Alert (' B ');
},false)
Above you can see the use of event binding, the difference between the event is the use of the onclick one is the click, so the FF and Chrome events are not on, and IE must be on the
Encapsulation functions are required due to compatibility issues
function Addevent (OBJ,EV,FN) {
if (obj.attachevent) {
For IE browser
Obj.attachevent (' on ' +ev,fn)
}else{
For FF and Chrome
Obj.addeventlistener (Ev,fn,false)
}
}
So the above example can be changed to write
Addevent (obtn, ' click ', function () {
Alert (' a ');
})
Addevent (obtn, ' click ', function () {
Alert (' B ');
})
However, if this is present in the function
For example:
Addevent (obtn, ' click ', function () {
alert (this);
})
The results are different. This is the window that pops up in IE, which is the current object in FF and Chrome.
If you want to use the current object, you need to further transform the function
Addevent (obtn, ' click ', function (EV) {
var oevent = ev| | Event
var that=oevent.srcelement| | Oevent.target;
Alert (that)
})
- Title: Issues related to compatibility in attachevent and AddEventListener in native event bindings and the This reference window in attachevent functions
- This question address: http://www.educity.cn/wenda/146927.html
Attachevent and AddEventListener compatibility