📜  计数器 javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:12.609000             🧑  作者: Mango

代码示例2
//simple counter in javascript 

// this is button element variable
const button = document.querySelectorAll(".btn")

//this variable for display counter number
const h3 = document.querySelector(".counter")
//Count variable
let count = 0;

//foreach method
button.forEach((btn) => {
  //event listener function       
  btn.addEventListener("click", (e) => {
    //event targeting element  
    let target = e.currentTarget

    //condition for counter variable
    if (target.innerText == "reset") {
      count = 0;
    }
    else if (target.innerText == "next") {
      count++;
    }
    else if (target.innerText == "prev") {
      count--;
    }
    //text contains according to the if condition
    h3.innerHTML = count;

  })

})