我试图理解为什么我无法从底部的i
函数内部访问变量forEach()
。有人可以帮我吗?
draw() {
this.players.forEach( (player, i) => {
let cardElements = [];
player.cards.forEach( (card) => {
let cardElement = document.createElement("div")
cardElement.classList.add("card")
cardElement.setAttribute("suit", card.suit)
let cardValue = document.createElement("div")
cardValue.classList.add("value")
cardValue.textContent = card.value
cardElement.append(cardValue)
cardElements.push( cardElement )
});
cardElements.forEach( (cardElement) => {
document.getElementById(`player-${i}`).append(cardElement)
});
})
}
答案 0 :(得分:2)
字符串插值适用于${i}
而不是$i
document.getElementById(`player-${i}`).append(cardElement)