1 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
您應(yīng)該通過為每個(gè)元素提供正確的索引來隔離它們。
Player 是一個(gè)數(shù)組,因此您必須使用索引訪問元素
let player = document.querySelectorAll('.player')
這應(yīng)該有幫助:
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 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)