单击时更改按钮颜色

问题描述 投票:0回答:5

我希望我的

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>
javascript html button onclick global
5个回答
18
投票

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>

希望这有帮助。


9
投票

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)" 
/>

([条件(三元)运算符])

Example-1

Example-2


0
投票

每次

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

}

0
投票

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

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

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

0
投票
<!DOCTYPE html>
<html>
    <head>
        <script language="javascript" type="text/javascript">
           var button = document.getElementById("txt");
           var color = document.getElementById("word").style.color

             function changeColor(color) {
                  color = "blue";
                     };
                button.onclick = changeColor();
        </script>
    </head>
<body>
<h1 id="word">Hello world</h1>
<button id="txt" onclick="changeColor()">Click here</button>
</body>
</html> 
© www.soinside.com 2019 - 2024. All rights reserved.