单击时切换按钮颜色

时间:2014-03-05 07:08:19

标签: javascript jquery html

这里我写了一个JavaScript来点击一次按钮的颜色变为绿色,当我再次点击按钮时,它的颜色应该变回橙色。按钮的默认颜色为橙色。我给出了rgb颜色值。但是,当我点击按钮时,它的颜色从橙色变为绿色,当我再次点击它时,它的颜色保持绿色不会变回橙色。请帮我解决这个问题。

<script>
function colorchange(id)
{

  var background = document.getElementById(id).style.background;

  if(background = "rgb(255,145,0)")
  {
  document.getElementById(id).style.background = "rgb(26,255,0)";
  }
  if(background == "rgb(26,255,0)")
  {
    document.getElementById(id).style.background = "rgb(255,145,0)";
  }

}
</script>

以下是输入按钮的HTML代码

<input type="button" name="Ignore Select" value="Ignore Select" id="select" onclick="colorchange('select')" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Delete" value="Ignore Delete" id="select1" onclick="colorchange('select1');" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Insert" value="Ignore Insert" id="select2" onclick="colorchange('select2');" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Update" value="Ignore Update" id="select3" onclick="colorchange('select3');" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Sleep" value="Ignore Sleep" id="select4" onclick="colorchange('select4');" style="background:rgb(255,145,0);"/>

6 个答案:

答案 0 :(得分:8)

应为if(background == "rgb(255,145,0)")

使用比较运算符==代替分配=

function colorchange(id) {

    var background = document.getElementById(id).style.backgroundColor;
    if (background == "rgb(255, 145, 0)") {
        document.getElementById(id).style.background = "rgb(26,255,0)";
    } else {
        document.getElementById(id).style.background = "rgb(255,145,0)";
    }

}

演示:Fiddle


因为使用了jQuery标签

<input type="button" name="Ignore Select" value="Ignore Select" id="select" class="select" />
<input type="button" name="Ignore Delete" value="Ignore Delete" id="select1" class="select" />
<input type="button" name="Ignore Insert" value="Ignore Insert" id="select2" class="select" />
<input type="button" name="Ignore Update" value="Ignore Update" id="select3" class="select" />
<input type="button" name="Ignore Sleep" value="Ignore Sleep" id="select4" class="select" />

然后

.select {
    background:rgb(255,145,0);
}
.select.highlight {
    background:rgb(26, 255, 0);
}

jQuery(function ($) {
    $('.select').click(function () {
        $(this).toggleClass('highlight')
    })
})

演示:Fiddle

答案 1 :(得分:2)

<强> JS FIDDLE

<强> Jquery的:

$(":button").on('click', function () {
    var gValue = $(this).attr('class');
    if (gValue == 'orange') {
        $(this).removeClass("orange");
        $(this).addClass("red");
    } else {
        $(this).removeClass("red");
        $(this).addClass("orange");
    }
});

<强> CSS:

.red {
    background-color:red;
}
.orange {
    background-color:orange;
}

答案 2 :(得分:1)

您应该comparison运营商而不是assignment运营商

if(background = "rgb(255,145,0)")

if(background == "rgb(255,145,0)")

//

 <script>
    function colorchange(id)
{

  var background = document.getElementById(id).style.backgroundColor;

  if(background == "rgb(255, 145, 0)")
  {
  document.getElementById(id).style.backgroundColor = "rgb(26,255,0)";
  }
  if(background == "rgb(26, 255, 0)")
  {
    document.getElementById(id).style.backgroundColor = "rgb(255,145,0)";
  }

}
    </script>

DEMO

答案 3 :(得分:0)

为什么不使用css作为背景颜色? http://jsfiddle.net/

HTML:

<input type="button" name="Ignore Select" value="Ignore Select" id="select" onclick="colorchange('select')" class="classA" />

    <script>
        function colorchange(id)
    {
      var el = document.getElementById(id);
      var currentClass = el.getAttribute("class");
      if(currentClass == 'classA')
      {
          el.setAttribute("class", "classB");
      } else {
         el.setAttribute("class", "classA");
      }

    }
    </script>

CSS:

.classA {
    background:rgb(255,145,0);
}

.classB {
   background:rgb(26,255,0); 
}

答案 4 :(得分:0)

我会这样写:

btn是你的按钮

var background = document.getElementById(id).style.background;
var btn = addEventListener("click", function (){
    if(background = "rgb(255,145,0)"){
        document.getElementById(id).style.background = "rgb(26,255,0)";
    }
    else
    {
        document.getElementById(id).style.background = "rgb(255,145,0)";
    }
});

答案 5 :(得分:0)

我的方法:

var eButton = document.querySelectorAll("button");
var isPink = false;//using white and pink in this example

eButton.addEventListener("click", function(){
   if(isPink){
      document.body.style.background = "white";
    }
   else{
      document.body.style.background = "pink";
    }
   isPink = !isPink; //switches between true and false
    });

OR

为背景编写CSS并使用classList属性

.pink{
background : pink;
}

JS

eButton.addEventListener("click", function(){
   document.body.classList.toggle("pink");
});