我有一个问题,一个div变化的背景,但我也需要它改变回来时,背景颜色已被应用。
代码: JavaScript:
function myFunction() {
if (document.getElementById("demo").style.background == "#ff77ee") {
document.getElementById("demo").style.background = "#000";
} else{
document.getElementById("demo").style.background = "#ff77ee";
}
}代码: HTML:
<div id="demo" onclick="javascript:myFunction();"></div>代码:样式:(在我的索引文件中)
#demo{
background: #000;
width: 200px;
height: 200px;
}我想要做的是,我想点击改变背景颜色为粉红色。然后,如果我再次点击它,我希望颜色是原来的颜色-黑色.。
发布于 2014-11-21 08:25:58
$(document).ready(function(){
$("#demo").click(function(){
$("#demo").css('background','red');
});
});
<div id="demo"> test </div>您可以简单地通过jquery需要添加jquery文件来完成它。
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>https://stackoverflow.com/questions/27056920
复制相似问题