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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

計(jì)數(shù)器在 Java 腳本中獲取最后一個(gè)增量值

計(jì)數(shù)器在 Java 腳本中獲取最后一個(gè)增量值

BIG陽 2023-05-11 14:13:20
我正在制作一個(gè)用于學(xué)習(xí)的計(jì)數(shù)器應(yīng)用程序,在給計(jì)數(shù)器名稱后,計(jì)數(shù)器在 onclick 事件中遞增 1 這在只有計(jì)數(shù)器時(shí)工作正常但如果我添加多個(gè)名稱它會(huì)在新計(jì)數(shù)器上采用最后一個(gè)增量值,基本上每個(gè)新創(chuàng)建的計(jì)數(shù)器的增量應(yīng)該是分開的。let counter = 0function updateCount() {  let progressCountUpdate = document.getElementsByClassName("progress-count")  for (var i = 0; i < progressCountUpdate.length; i++) {    progressCountUpdate[i].onclick = function () {      this.childNodes[1].innerText = counter++    }  }}function createProgress() {  progressCount = document.createElement("div")  progressCount.className = "progress-count"  progressNameNew = document.createElement("span")  progressNameNew.innerText = progresslabel.value;  progressNameNew.className = 'progress-name'  progressCountNumber = document.createElement("span")  progressCountNumber.innerText = 0  progressCountNumber.className = "progress-counter"  progressCount.appendChild(progressNameNew)  progressCount.appendChild(progressCountNumber)  progressDisplay.appendChild(progressCount)  progresslabel.value = ""  updateCount()}addbutton.onclick = function () {  if (progresslabel.value === "") {    alert("enter progress name")  } else {    checkStartEndTime();    createProgress();  }}<div class="progress-display"></div>如何確保每個(gè)計(jì)數(shù)器的增量都是獨(dú)立的。
查看完整描述

1 回答

?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

您可以創(chuàng)建一個(gè)閉包函數(shù),以便為每個(gè)單獨(dú)的onclick函數(shù)添加一個(gè)計(jì)數(shù)器,如下所示:


function updateCount() {

  let progressCountUpdate = document.getElementsByClassName("progress-count");


  for (let el of progressCountUpdate) {

    if (el.getAttribute('hasOnClick') !== 'true') {

      el.onclick = (function() {

        const childNode = el.childNodes[1];

        let counter = 0;


        el.setAttribute('hasOnClick', 'true'); // set a flag so it won't reset every update


        return function() {

          return childNode.innerText = counter++;

        }

      })();

    }

  }

}


查看完整回答
反對(duì) 回復(fù) 2023-05-11
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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