我写了下面的代码和2个问题:1)我想当光标悬停在我的div上有详细信息类,颜色改变为整个div但在我的内部div的代码中没有这样做.2)为所有div编写的jquery行几乎是相同的,我有很多这些div.我可以编写代码简介以避免重复吗?
请你能告诉我怎么做!
$(".detail-1").hover(function() {
$(".detail-1 div:first-child").css("background-color", "green");
$(".detail-1 div:nth-child(2) p").css("color", "blue");
})
$(".detail-1").mouseout(function() {
$(".detail-1 div:first-child").css("background-color", "#41b5e7");
$(".detail-1 div:nth-child(2) p").css("color", "black");
})
$(".detail-2").hover(function() {
$(".detail-2 div:first-child").css("background-color", "yellow");
$(".detail-2 div:nth-child(2) p").css("color", "blue");
})
$(".detail-2").mouseout(function() {
$(".detail-2 div:first-child").css("background-color", "#41b5e7");
$(".detail-2 div:nth-child(2) p").css("color", "black");
})
$(".detail-3").hover(function() {
$(".detail-3 div:first-child").css("background-color", "pink");
$(".detail-3 div:nth-child(2) p").css("color", "blue");
})
$(".detail-3").mouseout(function() {
$(".detail-3 div:first-child").css("background-color", "#41b5e7");
$(".detail-3 div:nth-child(2) p").css("color", "black");
})
.details {
width: 200px;
height: 90px;
border: 1px solid #333;
margin-top: 10px;
}
.details > div {
float: left;
}
.details > div > p {
line-height: 15px;
padding-left: 10px;
}
.details div:first-child {
width: 70px;
height: 70px;
background-color: #41b5e7;
margin: 10px 0px 0px 10px;
}
text-1
Description-1
text-2
Description-2
text-3
Description-3
在此不需要jQuery,您可以使用此CSS执行以下操作.
最好的方法是向div添加类,并以这种方式定位它们.但如果您无法编辑代码,请查看@ RoryMcCrossan的答案.
因为虽然CSS中的伪选择器很有用,当然可以按照你的方式使用,但是尽可能使用id和类会好得多.这也意味着更容易遵循代码.
首先,将一个类添加到更改颜色的div,并添加到包含文本的div,如下所示:
然后使用CSS来定位这些:
.detail-1:hover .color-block { background: green; } .detail-1:hover .text-block { color: blue; }
示例代码段
.details {
width: 200px;
height: 90px;
border: 1px solid #333;
margin-top: 10px;
}
.details > div {
float: left;
}
.details > div > p {
line-height: 15px;
padding-left: 10px;
}
.details div:first-child {
width: 70px;
height: 70px;
background-color: #41b5e7;
margin: 10px 0px 0px 10px;
}
.detail-1:hover .color-block {
background: green;
}
.detail-1:hover .text-block {
color: blue;
}
text-1
Description-1