[英]How to iterate through an array on each click of an event listener?
I'm new to JS and need help.我是 JS 的新手,需要帮助。 I want to change an image and a paragraph dynamically with each click on an arrow.我想在每次单击箭头时动态更改图像和段落。 To do this, I have an array that contains the different values that this image and this paragraph must take on each click.为此,我有一个数组,其中包含此图像和此段落在每次单击时必须采用的不同值。
Here is my table:这是我的桌子:
const slides = [
{
"image":"slide1.jpg",
"tagLine":"Impressions tous formats <span>en boutique et en ligne</span>"
},
{
"image":"slide2.jpg",
"tagLine":"Tirages haute définition grand format <span>pour vos bureaux et events</span>"
},
{
"image":"slide3.jpg",
"tagLine":"Grand choix de couleurs <span>de CMJN aux pantones</span>"
},
{
"image":"slide4.png",
"tagLine":"Autocollants <span>avec découpe laser sur mesure</span>"
}
]
The idea is: I want to create a function to loop through my array on each click with a loop.这个想法是:我想创建一个 function 来在每次点击时循环遍历我的数组。
click 1 = object 1 of the array. click 2 = object 2 of the array. click 3 = object 3 of the array. click 4 = return to object 0.
let slide = 0
let currentSlide = slides[slide]
console.log(currentSlide)
// Function to advance in the table slides on click
function loopTableSlides() {
slides.forEach(function(slide, index) {
if ((slides.length - 1) == slide) {
slide = 0 // to return to the first object in the array
} else {
slide = index + 1 // to move on to the next item each turn
}
console.log(currentSlide)
})
}
const arrowRight = document.querySelector('.arrow_right')
arrowRight.addEventListener('click', function(e) {
e.stopPropagation()
console.log('arrow right click')
activeBullet()
loopTableSlides()
})
I then want to create another function with a setAttribute and an innerHTML which will be called in my event listener and which will fetch the values of this famous loop.然后我想创建另一个带有 setAttribute 和 innerHTML 的 function ,它将在我的事件侦听器中调用,并将获取这个著名循环的值。
Attached is the corresponding code with the first function I'm trying to check and the array in question.附件是我要检查的第一个 function 和有问题的数组的相应代码。 In the console, it returns 4 times the same object and always the same.在控制台中,它返回 4 次相同的 object 并且始终相同。 Unfortunately I don't understand why...不幸的是我不明白为什么...
Thanks for your help.谢谢你的帮助。
you can do something like this你可以这样做
const slides = [ { "image":"slide1.jpg", "tagLine":"Impressions tous formats <span>en boutique et en ligne</span>" }, { "image":"slide2.jpg", "tagLine":"Tirages haute définition grand format <span>pour vos bureaux et events</span>" }, { "image":"slide3.jpg", "tagLine":"Grand choix de couleurs <span>de CMJN aux pantones</span>" }, { "image":"slide4.png", "tagLine":"Autocollants <span>avec découpe laser sur mesure</span>" } ] const slide = document.getElementById('slide') const prev = document.getElementById('prev') const next = document.getElementById('next') let index = 0 const showSlide = n => { slide.innerHTML = slides[n].image } showSlide(index) next.addEventListener('click', () => { index = (index + 1) % slides.length showSlide(index) }) prev.addEventListener('click', () => { index = (index + slides.length - 1) % slides.length showSlide(index) })
<div id="slide"></div> <button id="prev" >prev</button> <button id="next" >next</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.