所以我有一个字符串数组会变成按钮,
//At start
function acceptSuggestion() {
console.log(`clicked`)
console.log(this.textContent);
}
//Else where
suggestions.couldBe.innerHTML = ``;
list.suggestions.forEach(function (item) {
let button = document.createElement(`button`);
button.textContent = item;
button.addEventListener(`click`, acceptSuggestion);//before append
button.style = `text-align:center; width:50%`;
suggestions.couldBe.appendChild(button);
button.addEventListener(`click`, acceptSuggestion);//after append
suggestions.couldBe.innerHTML+=`<br>`;
});
为什么是这样?我知道我有这个事件的权利:https ://www.w3schools.com/js/js_htmldom_eventlistener.asp
如果重要的话,我正在使用 electron.js 创建一个类似应用程序的网页,而不是浏览器。
