addEventListener is not working

2019-06-02 04:39发布

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');
}
}

3条回答
Bombasti
2楼-- · 2019-06-02 05:23

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).

查看更多
欢心
3楼-- · 2019-06-02 05:32

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

You can use attachEvent() in IE.

查看更多
Anthone
4楼-- · 2019-06-02 05:33

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

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

查看更多
登录 后发表回答