【发布时间】:2019-11-18 14:57:12
【问题描述】:
我不是 JS 专家。我让这个功能工作得很好,但它在 IE 中不起作用。我认为这是因为使用 'let' 的循环函数,然后在查询当前循环项的索引的函数中......我知道这在旧浏览器中可能不支持。
但是,如果没有该部分,我不知道如何获得相同的结果...我找到了这个
var g = document.getElementById('my_div');
for (var i = 0, len = g.children.length; i < len; i++)
{
(function(index){
g.children[i].onclick = function(){
alert(index) ;
}
})(i);
}
来自Get index of clicked element using pure javascript
但不确定如何使用它。感谢您的帮助!
我的功能:
window.onload = function(){
const openers = document.querySelectorAll('.openerbuttons');
const fullsections = document.querySelectorAll('.fullsection');
for(let i = 0; i < openers.length; i++){
openers[i].addEventListener('click',function(){
if(!fullsections[i].classList.contains('inview')){
openers.forEach(function(opener) {
opener.classList.remove('opened');
});
fullsections.forEach(function(fullsectionjs) {
fullsectionjs.classList.remove('inview');
});
openers[i].classList.add('opened');
fullsections[i].classList.add('inview');
} else{
openers[i].classList.remove('opened');
fullsections[i].classList.remove('inview');
}
});
}
}
【问题讨论】:
-
从这个链接尝试你的代码的这个转换版本,以检查它是否工作。 textuploader.com/1o9rv 可以使用 babel 转译代码。 babeljs.io
-
您可以查看 MDN 文档以查看您使用的方法是否兼容。 Here's the listing for
classListfor example - (不兼容)。该页面还包含可用于修补代码的 polyfill。
标签: javascript loops internet-explorer