我刚刚开始探索 Jquery,实际上我正在尝试实现我的第一个设计。
问题是,我希望(在这个简单的例子中)三个 div 框通过单击其中一个 div 来改变它们的类,这似乎只可能一次!
同时我希望它是动态的,所以我使用了“.switchClass”(在“.toggleClass”和“.removeClass().addClass() 之后)和jquery UI 来进行可见的转换。
如果可以向我解释如何多次切换课程,那就太好了。
请原谅我的地下编码...我是新手。
HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="testgetid.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<title>keepitclear</title>
</head>
<body>
<div id="wrapper">
<div id="green" class="Aindex">A</div>
<div id="red" class="Bindex">B</div>
<div id="blue" class="Cindex">C</div>
</div>
<script>
$('#green').click(function () {
$(this).toggleClass('Aaaaaa');
$('#red').toggleClass('Baaaaa');
$('#blue').toggleClass('Caaaaa');
});
$('#red').click(function () {
$(this).toggleClass('Bbbbbb');
$('#green').toggleClass('Abbbbb');
$('#blue').toggleClass('Cbbbbb');
});
$('#blue').click(function () {
$(this).toggleClass('Cccccc');
$('#green').toggleClass('Accccc');
$('#red').toggleClass('Bccccc');
});
</script>
</body>
</html>
CSS(必需,因为很好):
body{
background-color: black;
}
#wrapper{
margin: 0 auto;
width: 1280px;
height: 1024px;
overflow: hidden;
background-color: white;
}
.Aindex{
position: absolute;
margin-top: 100px;
margin-left: 300px;
width: 100px;
height: 100px;
background-color: #53D35F;
cursor: pointer;
}
.Aaaaaa{
position: absolute;
margin-top: 0px;
margin-left: 250px;
width: 200px;
height: 200px;
background-color: #99F748;
}
.Abbbbb{
position: absolute;
margin-top: 125px;
margin-left: 350px;
width: 50px;
height: 50px;
background-color: #287F28;
}
.Accccc{
position: absolute;
margin-top: 125px;
margin-left: 275px;
width: 50px;
height: 50px;
background-color: #287F28;
}
.Bindex{
position: absolute;
margin-top: 200px;
margin-left: 250px;
width: 100px;
height: 100px;
background-color: #F48725;
}
.Baaaaa{
position: absolute;
margin-top: 175px;
margin-left: 225px;
width: 50px;
height: 50px;
background-color: #9E2B15;
}
.Bbbbbb{
position: absolute;
margin-top: 150px;
margin-left: 200px;
width: 200px;
height: 200px;
background-color: #F4dC76;
}
.Bccccc{
position: absolute;
margin-top: 150px;
margin-left: 250px;
width: 50px;
height: 50px;
background-color: #9E2B15;
}
.Cindex{
position: absolute;
margin-top: 200px;
margin-left: 350px;
width: 100px;
height: 100px;
background-color: #1FA2FF;
}
.Caaaaa{
position: absolute;
margin-top: 175px;
margin-left: 425px;
width: 50px;
height: 50px;
background-color: #4F35D3;
}
.Cbbbbb{
position: absolute;
margin-top: 175px;
margin-left: 375px;
width: 50px;
height: 50px;
background-color: #4F35D3;
}
.Cccccc{
position: absolute;
margin-top: 150px;
margin-left: 275px;
width: 200px;
height: 200px;
background-color: #1FFFFA;
}