javascript - 使用 JavaScript 使用 FOR OF 数组循环时出现错误?

标签 javascript jquery html internet-explorer

这里,为什么我的代码在 IE 中不起作用。 我的代码适用于所有浏览器。没有问题。 但是当我在 IE 上运行我的项目时,它发现错误。

而且我的 jquery 类和 insertadjacentHTMl 也不起作用。

我在这段代码中发现的主要问题。

$("[type=slideshow] section").addClass('hide');
		for(let ele of Array.from($("[type=slideshow]"))){
			$(ele).children("section:first").removeClass("hide").addClass('active');
		}

这是我的代码,请检查哪里有错误。

//Code for Slideshow

var divSlide = document.querySelectorAll('#slide');

var myNodeList = divSlide.length;
let slideNo = 1;

for(var i = 0; i < myNodeList; i++) {
	var type = divSlide[i].getAttribute("type");
	if (type == "timeline") {

	} else if (type == "slideshow") {
		var timeline = divSlide[i];     
		let sliderData = timeline.getElementsByTagName("section");
		
		$("[type=slideshow] section").addClass('hide');
		for(let ele of Array.from($("[type=slideshow]"))){
			$(ele).children("section:first").removeClass("hide").addClass('active');
		}

		timeline.insertAdjacentHTML("afterbegin",'<a class="left prev color_arrow carousel-control" href="#myCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span><span class="sr-only">Previous</span></a>');

		timeline.insertAdjacentHTML('afterbegin','<a class="right next color_arrows  carousel-control" href="#myCarousel"  data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span><span class="sr-only">Next</span></a>');
	}
}

$(document).on ('click','.prev',function() {
	let select = $(this).parent();
	let totChild = select.children("section");
	for(let i=0;i<totChild.length;i++){
		if(totChild[i].getAttribute('class').indexOf('active')!=-1){
			slideNo=i+1;
		}
	}
	totChild.children('br').remove();
	let current = select.children('.active');
	let prevEl = current.prev('section');
	
	if(slideNo === totChild.length || slideNo > 1){
		select.children(".next").show();
		if(prevEl.length !== 1){
			prevEl = current.prev().prev();
			current.removeClass('active');
			current.addClass('hide');

			prevEl.addClass('active');
			prevEl.addClass('animated');
			prevEl.addClass('fadeInLeft');
			prevEl.removeClass('hide');
		}
	} else {
		select.children(".prev").hide();
	}
});
$(document).on ('click','.next',function() {
	let select = $(this).parent();
	let totChild = select.children("section");
	for(let i=0;i<totChild.length;i++){
		if(totChild[i].getAttribute('class').indexOf('active')!=-1){
			slideNo=i+1;
		}
	}
	
	totChild.children('br').remove();
	let current = select.children('.active');
	let prevEl = current.next('section');
	if(slideNo ===1 || slideNo < totChild.length){
		
		select.children(".prev").show();
		if(prevEl.length !== 1){
			prevEl = current.next().next();
			current.removeClass('active');
			current.addClass('hide');
			prevEl.addClass('animated');
			prevEl.addClass('fadeInRight');
			prevEl.addClass('active');
			prevEl.removeClass('hide');
		}
	} else {
		select.children(".next").hide();
	}
});

最佳答案

for-of IE 不支持。您可以将 for-of-loop 替换为 jquery 的 each 作为

$("[type=slideshow] section").addClass('hide');
$("[type=slideshow]").each( function(i,ele){ //i is the index and ele is the element in iteration
   $(ele).children("section:first").removeClass("hide").addClass('active');
});

此外,无需将 jquery 与 javascript 混合 querySelector 并使用 for 循环循环结果。

当使用基于#的选择器#slide时,如本行所示

var divSlide = document.querySelectorAll('#slide');

它只会给出一个结果,只需使用

timeline = $( "#slide" )[0];

不需要外部for循环if条件

关于javascript - 使用 JavaScript 使用 FOR OF 数组循环时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728906/

相关文章:

javascript - 在页面之间传递 JavaScript 变量工作正常,但 DataTables 仍然出错

javascript - Jquery - 如何检查 div 中的所有输入是否不为空?

javascript - 如何使用 HTML 中的按钮播放动画?

php - 用户注册模块向PHP数据库插入空白数据

javascript - 基于字符串属性将对象数组转换为嵌套对象数组?

javascript - Firebase:通过 REST 获取 token 并使用 signInWithCustomToken

jquery - jqgrid内联添加

jquery - fancybox 2.0.6 ie7 不工作

html - 有没有办法在 :nth-child(n)? 中使用 "n"值

javascript - SEO - 显示 block anchor 标记包含标题和短描述与仅带有 js 的标题