如何使此 JavaScript 函数也与 Internet Explorer 兼容
How to make this JavaScript function also compatible with Internet Explorer
我不是 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');
}
});
}
}
多亏了评论中的建议,以及直接在 IE 11 中进行的故障排除(甚至不知道它有一个控制台),我用这段代码解决了这个问题。 forEach 方法也不被支持,所以我也改变了它。
请注意,classList 在 IE 11 中有所支持,甚至 classList.contains 也可以。
'use strict';
window.onload = function () {
var openers = document.querySelectorAll('.openerbuttons');
var fullsections = document.querySelectorAll('.fullsection');
var _loop = function _loop(i) {
openers[i].addEventListener('click', function () {
if (!fullsections[i].classList.contains('inview')) {
for (var j = 0, len = openers.length; j < len; j++) {
openers[j].classList.remove('opened');
fullsections[j].classList.remove('inview');
}
openers[i].classList.add('opened');
fullsections[i].classList.add('inview');
} else {
openers[i].classList.remove('opened');
fullsections[i].classList.remove('inview');
}
});
};
for (var i = 0; i < openers.length; i++) {
_loop(i);
}
};
我不是 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');
}
});
}
}
多亏了评论中的建议,以及直接在 IE 11 中进行的故障排除(甚至不知道它有一个控制台),我用这段代码解决了这个问题。 forEach 方法也不被支持,所以我也改变了它。
请注意,classList 在 IE 11 中有所支持,甚至 classList.contains 也可以。
'use strict';
window.onload = function () {
var openers = document.querySelectorAll('.openerbuttons');
var fullsections = document.querySelectorAll('.fullsection');
var _loop = function _loop(i) {
openers[i].addEventListener('click', function () {
if (!fullsections[i].classList.contains('inview')) {
for (var j = 0, len = openers.length; j < len; j++) {
openers[j].classList.remove('opened');
fullsections[j].classList.remove('inview');
}
openers[i].classList.add('opened');
fullsections[i].classList.add('inview');
} else {
openers[i].classList.remove('opened');
fullsections[i].classList.remove('inview');
}
});
};
for (var i = 0; i < openers.length; i++) {
_loop(i);
}
};