addEventListener is not working

2019-06-02 05:07发布

问题:

somehow addEventListener is not working for me. I am using following function. It shows me number of lines in the alert at line 8 but never says completed... Can anyone please tell the reason. I am using IE.

function load() {   
var lnk = new Array();
lnk = document.getElementsByTagName("a");
var len = lnk.length;
alert('inside for..length is ..'+len);

for (var i=0;i<len;i++){
alert(i+" "+lnk[i]);
    lnk[i].addEventListener('click',callMe,false);
        alert('completed');
}
}

回答1:

Internet Explorer doesn't support addEventListener until version 9. Previous versions use the proprietary attachEvent.

It is generally a good idea to use a library that abstracts browser differences (or a more general library such as YUI or jQuery).



回答2:

Internet Explorer does not implement the "addEventListener()" API :-)

You can use attachEvent() in IE.



回答3:

That is because each browser do it differently (thats the original reason js libraries where created). Read this

IE uses element.attachEvent('onclick',doSomething)