第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按鈕點擊事件監(jiān)聽器只調(diào)用一次

按鈕點擊事件監(jiān)聽器只調(diào)用一次

DIEA 2023-04-14 16:26:10
我有一個包含多個按鈕的類,用戶可以從中選擇一個數(shù)字,但是在點擊偵聽器觸發(fā)一次后,它不再被調(diào)用。代碼筆 ( https://codepen.io/jasonws/pen/qBZeMMB ) 是一個顯著簡化的版本,它仍然顯示問題。任何人都可以看看這個并讓我知道我做錯了什么嗎?謝謝!class Utilities {    selectedSession = "Second";    constructor () {        const self = this;        this.manage();        $("button").on("click", function(e) {            self.message("Button click: switching to " + this.value + ".");            self.selectedSession = this.value;            self.manage();        });    }    manage() {        const self = this;        const sessions = [ "First", "Second", "Third", "Fourth", "Fifth" ]        var code;        $("#picker").empty();        this.message("Manage: " + this.selectedSession + " is selected.");        sessions.forEach(function(entry) {            code = "<button type='button' value = '" + entry + "'";            if (entry == self.selectedSession) { code += " style='background-color: gray;'"}            code += ">" + entry + "</button>";            $("#picker").append(code);        });        $("#content").text($("#picker")[0].outerHTML);    }    message(text) {      if ($("#messages").text() != "") { $("#messages").append("<br>"); }      $("#messages").append(text);    }}test = new Utilities();<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="picker"></div>    <textarea id="content" rows="10" cols="50"></textarea>    <div id="messages"></div>
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻(xiàn)1842條經(jīng)驗 獲得超13個贊

這是因為您在每次單擊時重新創(chuàng)建按鈕 HTML 內(nèi)容,這會刪除所有附加的事件偵聽器。您可以在單擊后重新附加它們,或者通過修改現(xiàn)有元素來代替 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();


查看完整回答
反對 回復(fù) 2023-04-14
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號