1 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
這是因?yàn)槟诿看螁螕魰r(shí)重新創(chuàng)建按鈕 HTML 內(nèi)容,這會(huì)刪除所有附加的事件偵聽(tīng)器。您可以在單擊后重新附加它們,或者通過(guò)修改現(xiàn)有元素來(lái)代替 HTML 內(nèi)容調(diào)整按鈕狀態(tài)。
這是工作版本
class Utilities {
selectedSession = "Second";
constructor () {
const self = this;
this.init();
this.manage();
$("button").on("click", function(e) {
self.message("Button click: switching to " + this.value + ".");
self.selectedSession = this.value;
self.manage();
});
}
manage(){
this.message("Manage: " + this.selectedSession + " is selected.");
var buttons = $("button")
for(var x = 0; x < buttons.length; x++){
var button = buttons[x]
if(button.value === this.selectedSession){
button.style.backgroundColor = "gray"
} else {
button.style.backgroundColor = ""
}
}
}
init() {
const self = this;
const sessions = [ "First", "Second", "Third", "Fourth", "Fifth" ]
var code;
$("#picker").empty();
sessions.forEach(function(entry) {
var buttonHtml = `<button type='button' value='${entry}' >${entry}</button>`
$("#picker").append(buttonHtml);
});
$("#content").text($("#picker")[0].outerHTML);
}
message(text) {
if ($("#messages").text() != "") { $("#messages").append("<br>"); }
$("#messages").append(text);
}
}
test = new Utilities();
添加回答
舉報(bào)