IE11 中的 JS ForEach 循环 [英] JS ForEach Loops in IE11
本文介绍了IE11 中的 JS ForEach 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 IE11 中让 JS 循环处理页面上的 4 个元素时遇到问题.我希望函数 hideImg
在鼠标悬停在您悬停的元素上时运行.
I am having issues getting a JS loop to work over 4 elements on a page in IE11. I want the function hideImg
to run on mouseover on the element that you hovered over.
这是我的代码:
elements.forEach( function(element) {
element.addEventListener('mouseover', hideImg);
});
我想我发现 IE 不支持 forEach
循环,如何在纯 JS 中轻松将其转换为 for
循环?
I think I've found that forEach
loops are not supported in IE, how can I easily convert this to a for
loop in plain JS?
亲切的问候,
史蒂夫
Kind regards,
Steve
推荐答案
你可以这样做:
var elements = document.getElementsByClassName("test");
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener('mouseover', hideImg);
}
function hideImg() {
console.log("hideImg called")
}
.test {
width: 40px;
height: 20px;
border: green solid 1px;
}
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
这篇关于IE11 中的 JS ForEach 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文