简体   繁体   English

Javascript 点击更改背景颜色和计数点击

[英]Javascript click to change background color and counting click

I have a problem.我有个问题。 I have code as below:我有如下代码:

const btnHappyOne = document.getElementById("happy-btn-1");
const btnHappyTwo = document.getElementById("happy-btn-2");
const btnHappyThree = document.getElementById("happy-btn-3");

function changeHappyBgColor(e){
    let happyColor = e.target.style.backgroundColor;
    if (happyColor != "red") {
        e.target.style.backgroundColor = "red";     
    }
    else {

        e.target.style.backgroundColor = "";
        
    }
}

btnHappyOne.addEventListener('click', changeHappyBgColor);
btnHappyTwo.addEventListener('click', changeHappyBgColor);
btnHappyThree.addEventListener('click', changeHappyBgColor);

This code change color of button when I click.当我单击时,此代码会更改按钮的颜色。 My problem is that I want add some codes to count number of button whenever it change to red.我的问题是我想添加一些代码来计算按钮变为红色时的数量。 When I click to change background color to none, the number of button also degrease.当我单击将背景颜色更改为无时,按钮的数量也会脱脂。 Thank you.谢谢你。

Add var number=0;添加var number=0; below consts, in if block use number++ ;在 consts 之下,在 if 块中使用number++ and in else block use number-- ;并在 else 块中使用number-- You can track it in console to see the value, console.log(number);您可以在控制台中跟踪它以查看值, console.log(number); I hope this is what are you asking for我希望这是你要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM