Change Button color onClick

前端 未结 4 424
渐次进展
渐次进展 2020-11-29 04:59

I want my Button to change color every time I click on it. But it only changes color on the first click.

I believe the problem is in the setColor

4条回答
  •  青春惊慌失措
    2020-11-29 05:30

    Every time setColor gets hit, you are setting count = 1. You would need to define count outside of the scope of the function. Example:

    var count=1;
    function setColor(btn, color){
        var property = document.getElementById(btn);
        if (count == 0){
            property.style.backgroundColor = "#FFFFFF"
            count=1;        
        }
        else{
            property.style.backgroundColor = "#7FFF00"
            count=0;
        }
    
    }
    

提交回复
热议问题