gpt4 book ai didi

javascript - 在 Javascript 中切换 的颜色

转载 作者:行者123 更新时间:2023-12-01 00:09:57 27 4
gpt4 key购买 nike

我制作了一个 2D 正方形,可以在“类”之间切换以改变颜色。单击后,它会变成蓝色。再次单击时,它会变回灰色。代码如下!

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
.square {
display: inline-block;
border-radius: 4px;
height: 100px;
width: 100px;
text-align: center;
background: #D2D7D3;
}

.blue {
background: #446CB3;
}
</style>
</head>

<body>
<div class="square" id="b1"></div>

<script type="text/javascript">
<!-- changes square color -->
$("#b1").click(function() {
$('#b1').toggleClass('blue');
});
</script>

</body>

</html>

现在我正在尝试使用 3D 方 block 复制此切换功能,但尚未成功。因此,当单击它时,它应该变成蓝色,并保持蓝色,直到再次单击。然后,当再次单击时,它应该变回灰色。 (就像在 2D 版本中一样。)下面是我当前的非工作代码!

预先感谢您的任何建议!

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
.square {
display: inline-block;
border-radius: 4px;
height: 100px;
width: 100px;
text-align: center;
}

.square a {
color: #B6BBB7;
font-family: Helvetica, sans-serif;
font-weight: bold;
font-size: 36px;
text-align: center;
text-decoration: none;
background-color: #B6BBB7;
display: block;
position: relative;
padding: 20px 40px;

-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
filter: dropshadow(color=#000, offx=0px, offy=1px);

-webkit-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
-moz-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;

-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}

.square a:active {
top: 10px;
color: #446CB3;
background-color: #446CB3;

-webkit-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
-moz-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3pxpx 0 #003D7E;
box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
}
</style>
</head>

<body onload="pageLoad();">
<div id="experiment">
<div id="shape_container">
<div ontouchstart="" class="square" id="b1"><a href="#">b1</a></div>
</div>

<script type="text/javascript">
$("#b1").click(function() {
// haven't figured out how to write correct toggle code here!
});
</script>
</body>

</html>

最佳答案

不要以 :active 状态为目标,而是将其添加为类:

$("#b1").click(function() {
$(this).children('a').toggleClass('active');
});
.square {
display: inline-block;
border-radius: 4px;
height: 100px;
width: 100px;
text-align: center;
}

.square a {
color: #B6BBB7;
font-family: Helvetica, sans-serif;
font-weight: bold;
font-size: 36px;
text-align: center;
text-decoration: none;
background-color: #B6BBB7;
display: block;
position: relative;
padding: 20px 40px;

-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
filter: dropshadow(color=#000, offx=0px, offy=1px);

-webkit-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
-moz-box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;
box-shadow: inset 0 1px 0 #D2D7D3, 0 10px 0 #505551;

-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}

.square a.active {
top: 10px;
color: #446CB3;
background-color: #446CB3;

-webkit-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
-moz-box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3pxpx 0 #003D7E;
box-shadow: inset 0 1px 0 #B6D5FF, inset 0 -3px 0 #003D7E;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="experiment">
<div id="shape_container">
<div ontouchstart="" class="square" id="b1"><a href="#">b1</a></div>
</div>

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com