如何使此JavaScript函数也与Internet Explorer兼容
我不是JS方面的专家。我认为这是因为循环函数使用了'let',然后在函数中查询当前循环项的索引。。。我知道这在旧浏览器中可能不受支持 然而,我不知道如何实现同样的结果没有这一部分。。。我找到了这个如何使此JavaScript函数也与Internet Explorer兼容,javascript,loops,internet-explorer,Javascript,Loops,Internet Explorer,我不是JS方面的专家。我认为这是因为循环函数使用了'let',然后在函数中查询当前循环项的索引。。。我知道这在旧浏览器中可能不受支持 然而,我不知道如何实现同样的结果没有这一部分。。。我找到了这个 var g = document.getElementById('my_div'); for (var i = 0, len = g.children.length; i < len; i++) { (function(index){ g.children[i].onc
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);
}
var g=document.getElementById('my_div');
for(变量i=0,len=g.children.length;i
从
但我不确定如何使用它。谢谢你的帮助
我的职能:
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');
}
});
}
}
window.onload=function(){
const openers=document.queryselectoral('.openerbuttons');
const fullsections=document.queryselectoral('.fullsection');
for(设i=0;i
多亏了评论中的建议,以及IE 11中的故障排除(甚至不知道它有控制台),我用这段代码实现了这一点。forEach方法也不受支持,所以我也更改了它
请注意,在IE 11中,classList在某种程度上是受支持的,甚至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);
}
};
“严格使用”;
window.onload=函数(){
var openers=document.queryselectoral('.openerbuttons');
var fullsections=document.querySelectorAll('.fullsection');
var _loop=函数_loop(i){
开场白[i]。addEventListener('click',函数(){
如果(!fullsections[i].classList.contains('inview')){
for(var j=0,len=openers.length;j
从该链接尝试此传输版本的代码,以检查其是否正常工作。您可以使用babel传输代码。您可以查看MDN文档,查看您使用的方法是否兼容。-(不兼容)。该页面还包含可用于修补代码的polyfill。感谢您发布此问题的解决方案。我建议你试着在48小时后将自己的答案标记为这个问题的可接受答案,如果可以标记的话。它可以在将来帮助其他社区成员解决类似的问题。谢谢你的理解。