1 回答
TA贡献1906条经验 获得超3个赞
您应该通过为每个元素提供正确的索引来隔离它们。
Player 是一个数组,因此您必须使用索引访问元素
let player = document.querySelectorAll('.player')
这应该有帮助:
let isPlaying = false
let playBtn = document.querySelectorAll('.playerButton');
for (let i = 0; i < player.length; i++) {
if (playBtn[i]) {
playBtn[i].addEventListener('click', ()=>togglePlay(i));
}
}
// Controls & Sounds Methods
// ----------------------------------------------------------
function togglePlay(i) {
let player = document.querySelectorAll('.player')
if (player[i].paused === false) {
player[i].pause();
isPlaying = false;
document.querySelector(".fa-pause")
.style.display = 'none';
document.querySelector(".fa-play")
.style.display = 'block';
} else {
player[i].play();
document.querySelector(".fa-play")
.style.display = 'none';
document.querySelector(".fa-pause")
.style.display = 'block';
isPlaying = true;
}
}
- 1 回答
- 0 关注
- 98 浏览
添加回答
举报