[英]Putting a AddEventListener on a button that is in InnerHTML in a for loop
Im am creating a page with some music, right now I am working on the 'explore' page.我正在创建一个包含一些音乐的页面,现在我正在处理“探索”页面。 And you will see about 10 cards with music randomized from a array.
您会看到大约 10 张带有音乐的卡片,这些卡片是从一个数组中随机抽取的。 Everything is going smoothly until right know.
一切顺利,直到知晓。 I added a button to start the music, but i can't seem to be able to put a AddEventListener on the button, i just made the cards with innerHTML (I know its not the best option but this project is still practice) And now I am not able to make it work on.
我添加了一个按钮来开始播放音乐,但我似乎无法在按钮上放置一个 AddEventListener,我只是用 innerHTML 制作了卡片(我知道这不是最好的选择,但这个项目仍在实践中)现在我无法让它继续工作。 If i put the event listener after the function with the innerHTML buttons.
如果我将事件侦听器放在带有 innerHTML 按钮的 function 之后。 I am only able to get 1.
我只能得到1。
function handleLoadHighlights(number) {
for (i = 0; i < 10; i++) {
let randomSongs = Math.floor(Math.random() * songsList.length);
let song = songsList[randomSongs];
let background = backgroundGradients[i];
audioFile.src = song.audio;
highlightsOutput.innerHTML += `
<div class="highlighted__song" style="${background.backgroundcolor} ${background.backgroundimg}">
<div class="highlighted__description">
<h3 class="highlighted__description--title">${song.title}</h3>
<p class="highlighted__description--para">${song.artist}</p>
</div>
<div class="highlighted__footer">
<button class="highlighted__button--play"><i class="fas fa-play-circle"></i></button>
<img class="highlighted__img" src="./assets/covers/${song.img}"></img>
</div>
</div>
`;
}
}
The function that shows me the randomized card with the song function给我看随机卡带歌
const playButton = document.querySelectorAll('.highlighted__button--play');
for(i = 0; i < 10; i++) {
playButton[i].addEventListener('click', handlePlayAudio);
}
My try on fixing it, just always returns the last one ofc我尝试修复它,总是返回最后一个 ofc
Add the index of the song as a data attribute of the button.添加歌曲的索引作为按钮的数据属性。
function handleLoadHighlights(number) { for (i = 0; i < 10; i++) { let randomSongs = Math.floor(Math.random() * songsList.length); let song = songsList[randomSongs]; let background = backgroundGradients[i]; audioFile.src = song.audio; highlightsOutput.innerHTML += ` <div class="highlighted__song" style="${background.backgroundcolor} ${background.backgroundimg}"> <div class="highlighted__description"> <h3 class="highlighted__description--title">${song.title}</h3> <p class="highlighted__description--para">${song.artist}</p> </div> <div class="highlighted__footer"> <button class="highlighted__button--play" data-song="${randomSongs}"><i class="fas fa-play-circle"></i></button> <img class="highlighted__img" src="./assets/covers/${song.img}"></img> </div> </div> `; } }
Then in handlePlayAudio()
, you can get this index from the clicked button.然后在
handlePlayAudio()
中,您可以从单击的按钮中获取此索引。
let songIndex = event.target.dataset.song; audioFile.src = songsList[songIndex].src;
event
is the argument to handlePlayAudio()
. event
是handlePlayAudio()
的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.