[英]All divs has background blue.When I click on any div , background color has to change to red for only that div clicked & not others
但是,当您单击div中的任何一个时,其他div仍然为蓝色,并且单击的div必须仅更改为红色。我们如何使用jquery实现?
.blue {
background:blue;
width:100px;
height:100px;
display:inline-block;
float:left;
margin-right:20px;
}
<!doctype html>
<html lang="en">
<body>
<div id="test1" class="blue"></div>
<div id="test2" class="blue"></div>
<div id="test3" class="blue"></div>
</body>
</html>
通过使用此关键字更改当前div的背景颜色
$(".blue").on("click", function() {
$(".blue").css("background-color","blue");
$(this).css("background-color","red");
});
用这个。
$(".blue").on("click", function() { $(".blue").css("background", "blue") $(this).css("background", "red") })
.blue { background:blue; width:100px; height:100px; display:inline-block; float:left; margin-right:20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test1" class="blue"></div> <div id="test2" class="blue"></div> <div id="test3" class="blue"></div>
您可以使用-
$('div').click(function(){
$('.blue').css("background", "blue");
$(this).css("background","red");
});
要么 -
$('.blue').click(function(){
$('.blue').css("background", "blue");
$(this).css("background","red");
});
它将更改您单击的div的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.