Attachevent and AddEventListener compatibility

Source: Internet
Author: User

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.
     
Total 0 people 0%
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

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.