Change Button color onClick

前端 未结 4 417
渐次进展
渐次进展 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:19

    Using jquery, try this. if your button id is say id= clickme

    $("clickme").on('çlick', function(){

    $(this).css('background-color', 'grey'); .......

    0 讨论(0)
  • 2020-11-29 05:21

    1.

    function setColor(e) {
      var target = e.target,
          count = +target.dataset.count;
    
       target.style.backgroundColor = count === 1 ? "#7FFF00" : '#FFFFFF';
       target.dataset.count = count === 1 ? 0 : 1;
       /* 
    
       () : ? - this is conditional (ternary) operator - equals 
    
       if (count === 1) {
          target.style.backgroundColor = "#7FFF00";
          target.dataset.count = 0;
       } else {
          target.style.backgroundColor = "#FFFFFF";
          target.dataset.count = 1;
       } 
       target.dataset - return all "data attributes" for current element, 
       in the form of object, 
       and you don't need use global variable in order to save the state 0 or 1
      */ 
    }
    
    
    <input 
      type="button" 
      id="button" 
      value="button" 
      style="color:white" 
      onclick="setColor(event)"; 
      data-count="1" 
    />
    

    2.

    function setColor(e) {
       var target = e.target,
           status = e.target.classList.contains('active');
    
       e.target.classList.add(status ? 'inactive' : 'active');
       e.target.classList.remove(status ? 'active' : 'inactive'); 
    }
    
    .active {
      background-color: #7FFF00;  
    }
    
    .inactive {
      background-color: #FFFFFF;
    }
    
    <input 
      type="button" 
      id="button" 
      value="button" 
      style="color:white" 
      onclick="setColor(event)" 
    />
    

    ([conditional (ternary) operator])

    Example-1

    Example-2

    0 讨论(0)
  • 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;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 05:35

    There are indeed global variables in javascript. You can learn more about scopes, which are helpful in this situation.

    Your code could look like this:

    <script>
        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;
            }
        }
    </script>
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题