2017-04-01 75 views
0

但是,当你点击的div中的任何一个,其他的div仍然是蓝色的,点击DIV必须改为红色only.How我们实现使用jQuery的?所有div有背景blue.When我点击任何DIV,背景颜色必须改变为红色只有DIV点击&不是别人

.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> 

回答

0

通过使用该关键字改变背景颜色当前的div

$(".blue").on("click", function() { 
$(".blue").css("background-color","blue"); 
$(this).css("background-color","red"); 
}); 
+0

如果你点击任何一个DIV,其变为红色,但再次,如果你在任何其他分区剩余的div单击应该是蓝色而不是红色。所以这没有帮助。 –

+0

@AsmitGhatge检查此琴:https://jsfiddle.net/aligoren/0j8fcbmz/ – Ali

+0

是现在的工作。由于 –

0

使用此。

$(".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>

0

您可以使用须─

$('div').click(function(){ 
$('.blue').css("background", "blue"); 
$(this).css("background","red"); 

});

或者 -

$('.blue').click(function(){ 
$('.blue').css("background", "blue"); 
$(this).css("background","red"); 

});

它将改变你点击了该div的颜色。