我正在尝试通过单击按钮来更改表格的背景和边框。我还尝试通过将鼠标悬停在按钮上来更改颜色。我让悬停工作,如果我先做的话。我遇到的问题是,当我单击按钮更改背景颜色时,我无法单击任何其他按钮更改为该特定按钮颜色。例如,我有四个按钮,蓝色、绿色、黄色、红色。如果我单击蓝色按钮,背景将变为蓝色,然后如果我选择单击其他颜色的按钮,它将不起作用,并且在单击任何按钮一次后,我的悬停将不再起作用。另外,如何减少耦合。如果我添加更多颜色按钮,我会按照我的速度,这只会等于更多的代码行。
<h1 class="underline">Choose a Background or Border Color</h1>
<div class="divCenter">
<div class="divTable" ></div>
</div></br>
<button id="button1">Blue</button>
<button id ="button2">Green</button>
<button id="button3">Yellow</button>
<button id ="button4">Red</button></br>
<button id="button5">Blue Border</button>
<button id ="button6">Green Border</button>
<button id="button7">Yellow Border</button>
<button id ="button8">Red Border</button></br>
<script>
$(document).ready(function()
{
$("#button1").click(function()
{
$(".divTable").attr("class","divTableBlue");
});
$("#button2").click(function()
{
$(".divTable").attr("class","divTableGreen");
});
$("#button3").click(function()
{
$(".divTable").attr("class","divTableBlue");
});
$("#button4").click(function()
{
$(".divTable").attr("class","divTableRed");
});
$("#button1").hover(function()
{
$(".divTable").addClass("divTableBlue");
},
function()
{
$(".divTable").removeClass("divTableBlue");
});
$("#button2").hover(function()
{
$(".divTable").addClass("divTableGreen");
},
function()
{
$(".divTable").removeClass("divTableGreen");
});
$("#button3").hover(function()
{
$(".divTable").addClass("divTableYellow");
},
function()
{
$(".divTable").removeClass("divTableYellow");
});
$("#button4").hover(function()
{
$(".divTable").addClass("divTableRed");
},
function()
{
$(".divTable").removeClass("divTableRed");
});
});
</script>
CSS是
.divTable
{
display:table;
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
background:grey;
border-style:solid;
border-width:5px;
}
.divTableBlue
{
display:table;
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
background:blue;
border-style:solid;
border-width:5px;
}
.divTableGreen
{
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
background:green;
border-style:solid;
border-width:5px;
}
.divTableYellow
{
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
background:yellow;
border-style:solid;
border-width:5px;
}
.divTableRed
{
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
background:red;
border-style:solid;
border-width:5px;
}
.divTableBlueBorder
{
display:table;
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
border-style:solid;
border-width:5px;
border-color:blue;
}
.divTableGreenBorder
{
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
border-style:solid;
border-width:5px;
border-color:green;
}
.divTableYellowBorder
{
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
border-width:5px;
border-style:solid;
border-color:yellow;
}
.divTableRedBorder
{
padding-top:10px;
padding-bottom:200px;
padding-left:10px;
padding-right:10px;
width:250px;
border-style:solid;
border-width:5px;
border-color:red;
}