📜  每次单击元素时递增计数器 - Javascript 代码示例

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

代码示例1
// increment counter each time the picture element is clicked
        var counter = 0;//Counter variable starts at 0
        const var_counter = document.querySelector("#out_count");//display the number of clicks
        const elem = document.querySelector("body > div > div.box.kat_kontainer > img");//select the picture
        elem.addEventListener("click", function () {//function to be performed on each click
            counter += 1;//Counter variable is incremented by one
            var_counter.innerHTML = counter;//Display the value of the counter in the display element
        });