单击后前后更改html按钮的背景色
Changing the background color of html buttons forward and backward after clicks
我是JavaScipt和HTML的新手,只需要它作为一个附带问题。我有一些按钮,我想在点击后更改它们的背景颜色。我可以用部分解决
<input class="choice" onclick="this.style.background='blue';" type="submit" value='V' id='0' />
但是,当第二次单击该按钮时,它应该会变回原来的状态。最好是像上面那样的一行语句。如果有任何帮助,我将不胜感激。
我把你的代码分解成单独的JS,这样更干净,更容易维护。
如果禁用该按钮,则它将停止接收点击事件。我想这不是你想要的。
在这里,我已经做到了,当点击按钮时,它会在红色和蓝色之间切换背景色。
http://jsbin.com/huqoyixuhe/edit?html,css,js,输出
// select the button
var input = document.querySelector('input.choice');
// add a click event listener
input.addEventListener('click', function () {
// if the button is red, make it blue
if (this.style.background === 'red') {
this.style.background = 'blue';
}
// otherwise, it is already blue, so make it red!
else {
this.style.background = 'red';
}
});
编辑
这里,作为一句话:http://jsbin.com/bolowuceqa/edit?html,输出
<input class="choice" onclick="this.style.background==='red' ? this.style.background = 'blue' : this.style.background = 'red'" style="background:red;" type="submit" value='V' id='0' />
您可以通过纯javascript在类名之间使用toggole。
HTML:
<button id="toggole" class="red" onclick="toggolecolor()">
Click Please
</button>
CSS:.red{
background-color:red;
}
.blue{
background-color:blue;
}
Javascript:
function toggolecolor(){
var element = document.getElementById("toggole");
element.classList.toggle("red");
element.classList.toggle("blue");
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<body bgcolor="(color)"/>
相关文章:
- jquery.each(第2行)添加背景色
- 如果使用javascript函数屏幕太小,我该如何更改HTML文件的背景色
- 悬停时淡入背景色
- 如何在单击单元格中的链接值时动态更改表行背景色
- Gridview内部的下拉框在回发时未保留背景色
- 用背景色智能填充引导列
- 带有背景色的高图表柱形图
- 带有javascript的选定文本的背景色
- 当代码使用背景色时无法更改位置
- 将元素放置在方框顶部时更改方框的背景色
- 检索表中元素的背景色
- 在Wordpress中创建随机背景色
- 存在事件的单元格的jQuery DatePicker背景色(加载时)
- CSS转换未在增加高度时更新背景色
- 如何设置按钮来更改按钮的背景色
- mmenu:覆盖点击按钮的背景色
- 启用段落的背景色<p>当有一个带有CSS的引导按钮时
- 单击后前后更改html按钮的背景色
- 一旦我们使用css点击单个按钮,如何更改其他两个按钮的不透明度背景色
- 多个状态按钮-背景色