切换按钮时更改按钮背景颜色 [英] Change button background color on toggle
本文介绍了切换按钮时更改按钮背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮,我想在单击时将其背景颜色更改为白色,然后当我再次单击时,它又返回其原始背景颜色,我该怎么做? 这是我的代码:
I have a button and i want to change its background color to white on click, and then when i click another time it return back to its original background color, how can i do that? here is my code:
$(document).ready(function () {
$("button").click(function () {
$(this).css("background-color", "#FFF");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Hello</button>
推荐答案
您需要做的就是添加某种类型的布尔布尔值,在本例中为名为"white"的变量.
All you need to do is add some sort of toggle boolean, in this example it's the var named 'white'.
var white = false
var bgcolor;
$(document).ready(function () {
$("button").click(function () {
if (white = !white) {
bgcolor = $(this).css('backgroundColor');
$(this).css("background-color", "#FFF");
} else {
$(this).css("background-color", bgcolor);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Hello</button>
这篇关于切换按钮时更改按钮背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文