1 回答

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
通過(guò)一些重構(gòu),成為問(wèn)題的是在 HTML 中,我在呈現(xiàn)數(shù)據(jù)之前調(diào)用了函數(shù),導(dǎo)致函數(shù)在 0 個(gè)對(duì)象的數(shù)組上工作,從而改變了我的按鈕單擊并將事件偵聽器添加到其余對(duì)象所在的位置正在初始化解決了這個(gè)問(wèn)題。下面是對(duì)我有用的更新的 HTML 和 JS 函數(shù)。
<div id="filter">
<div id="buttons">
<button id="sort">Sort Games Alphabetically</button>
</div>
</div>
document.getElementById("sort").addEventListener ("click", this.sortAlpha.bind(this));
console.log('test')
this.games.sort(function(gameA, gameB){
if (gameA.title < gameB.title) {
return -1;
}
if (gameA.title > gameB.title){
return 1;
}
return 0;
});
console.log(this.games)
const gamesContainer = document.getElementById('games-content')
gamesContainer.innerHTML = ""
this.renderGames()
}
}
添加回答
舉報(bào)