【发布时间】:2017-11-10 21:42:23
【问题描述】:
我正在尝试使绿色圆圈在单击时在红色和绿色之间交替。代码对我来说看起来不错,但它不起作用,所以一定有问题。我犯了什么初学者错误?
$("#greenCircle").click(function() {
console.log($("#greenCircle").css("background-color"));
if ($("#greenCircle").css("background-color") == "red") {
$("#greenCircle").css("background-color", "green");
} else {
$("#greenCircle").css("background-color", "red");
}
});
#greenCircle {
background-color: green;
height: 150px;
width: 150px;
border-radius: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="greenCircle"></div>
我快疯了!我只是在做一个简单的 jQuery 动画,但它再次不起作用。一切看起来都是正确的。我什至甚至切换了文本编辑器,什么也没有。这是动画的代码。
<!DOCTYPE html>
<html>
<head>
<title>JQuery</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="jQuery Practice.css">
</head>
<body>
hello
<div id="circle"></div>
<script type="text/javascript">
$("#circle").click(function(){
$("#circle").animate({Width:"400px"}, 2000);
});
</script>
</body>
#circle{
width: 150px;
height: 150px;
background-color: green;
border-radius: 50%;
}
【问题讨论】:
-
你有什么行为?
-
IIRC,使用 css 获取颜色字段的值将返回 rgb() 格式的值,而不是“红色”或“绿色”。使用最有可能的课程来做到这一点会更容易
标签: jquery css function animation onclick