gpt4 book ai didi

javascript - 在 for 循环中将变量名称与迭代器索引连接起来

转载 作者:行者123 更新时间:2023-11-30 14:04:03 26 4
gpt4 key购买 nike

我试图在 javascript 中创建一组按钮,使 slider 根据我单击的按钮交换图像。

我想到了这个丑陋的冗余代码:

var changer1 = document.getElementById("changer1");
var changer2 = document.getElementById("changer2");
var changer3 = document.getElementById("changer3");

var r = document.getElementsByClassName('carousel-item');

changer1.addEventListener('click', function() {
for (let i = 0; i < r.length; i++) {
r[i].classList.remove('active')
}
r[0].classList.add('active')
});

changer2.addEventListener('click', function() {
for (let i = 0; i < r.length; i++) {
r[i].classList.remove('active')
}
r[1].classList.add('active')
});

changer3.addEventListener('click', function() {
for (let i = 0; i < r.length; i++) {
r[i].classList.remove('active')
}
r[2].classList.add('active')
});

它成功了,但我知道可以优化冗余代码,所以我尝试了这个:

var changersArray = document.getElementsByClassName('changers');

for (let i = 0; i < 3; i++) {
changer[i].addEventListener('click', function() {
for (let i = 0; i < r.length; i++) {
r[i].classList.remove('active')
}
r[0].classList.add('active')
});
}

这是 HTML:

<section class="container-fluid sectionFive">
<div class="row title_image">
<div class="col-sm-12">
<h1 class="wow flipInY delay-1s slow">Testimonials</h1>
</div>
</div>

<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8 fetchDiv">

<div id="carouselExampleControls" class="carousel slide wow
fadeIn" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="mt-2 rounded-circle people" src="images/person_3.jpg" alt="">
<p class="frase"></p>
<p class="author"></p>

</div>
<div class="carousel-item">
<img class="mt-2 rounded-circle people" src="images/person_2.jpg" alt="">
<p class="frase"></p>
<p class="author"></p>

</div>
<div class="carousel-item">
<img class="mt-2 rounded-circle people" src="images/person_1.jpg" alt="">
<p class="frase"></p>
<p class="author"></p>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

</div>
<div class="col-sm-2">

</div>
</div>
<div class="row changersRow">
<button class="changers" id="changer1">1</button><button class="changers" id="changer2">2</button><button class="changers" id="changer3">3</button>

</div>
</section>

我试图通过在循环中写入 changer[i] 来动态创建 changer1、changer2 和 changer3,但没有成功。

拜托,你能帮我弄清楚为什么它不起作用,我怎样才能让它起作用?

谢谢。

最佳答案

前两个改进来 self 的脑海:

for (let i = 0; i < 3; i++) {  
changer[i].addEventListener('click', function() {
for (let j = 0; j < r.length; j++) {
r[j].classList.remove('active')
}
r[i].classList.add('active')
});
}

注意内部 i 被替换并且 r[0] 更改为使用实际索引

关于javascript - 在 for 循环中将变量名称与迭代器索引连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com