【问题标题】:Change Button color onClick更改按钮颜色 onClick
【发布时间】:2015-01-12 06:47:50
【问题描述】:

我希望我的Button 每次点击它时都会改变颜色。但它只会在第一次点击时改变颜色。

我相信问题出在setColor 函数中。每次我点击Button 时,count 都会被设置为 1。所以即使我将其设置为 0,它也会在下一次点击时重置为 1。我该如何解决? javascript/html 中是否有可以轻松解决此问题的全局变量?

<!DOCTYPE html>
<html>
<head>

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

}
</script>
</head>

<body>

<input type="button" id="button" value = "button" style= "color:white" onclick="setColor('button', '#101010')";/>

</body>
</html>

【问题讨论】:

  • 是的,将 var count=1 移到函数之前,它将是全局的。

标签: javascript html button onclick global


【解决方案1】:

javascript中确实存在全局变量。您可以了解更多关于 scopes 的信息,这对这种情况很有帮助。

您的代码可能如下所示:

<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>

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      【解决方案3】:

      每次setColor 被命中时,您都会设置 count = 1。您需要在函数范围之外定义 count。示例:

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

      【讨论】:

        【解决方案4】:

        使用 jquery,试试这个。 如果你的按钮 id 是 id= clickme

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-24
          • 1970-01-01
          • 2014-05-06
          • 2018-04-03
          • 1970-01-01
          相关资源
          最近更新 更多