简体   繁体   English

addEventListener 不工作

[英]addEventListener is not working

somehow addEventListener is not working for me.不知何故 addEventListener 对我不起作用。 I am using following function.我正在使用以下 function。 It shows me number of lines in the alert at line 8 but never says completed... Can anyone please tell the reason.它在第 8 行向我显示警报中的行数,但从未说完成...谁能告诉我原因。 I am using IE.我正在使用 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');
}
}

Internet Explorer doesn't support addEventListener until version 9. Previous versions use the proprietary attachEvent . Internet Explorer 直到版本 9 才支持 addEventListener。以前的版本使用专有的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 ).使用抽象浏览器差异的库(或更通用的库,例如YUIjQuery )通常是一个好主意。

Internet Explorer does not implement the "addEventListener()" API:-) Internet Explorer 没有实现“addEventListener()”API:-)

You can use attachEvent() in IE.您可以在 IE 中使用attachEvent()

That is because each browser do it differently (thats the original reason js libraries where created).那是因为每个浏览器的做法不同(这就是创建 js 库的最初原因)。 Read this读这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM