我想在点击div时获取其颜色,为此我创建了4个div并为每个div设置了不同的颜色,我想在点击它时获取div的颜色。 我得到的颜色是RGB格式的值,我想要十六进制格式的值,请帮助我。
请告诉我如何使用仅jQuery将我获得的RGB值转换为十六进制值。
<html>
<head>
<style>
#first {
background-color: red;
height: 50px;
width: 50px;
/*display: none;*/
}
#second {
background-color: green;
height: 50px;
width: 50px;
/*display: none;*/
}
#third {
background-color: yellow;
height: 50px;
width: 50px;
/*display: none;*/
}
#fourth {
background-color: blue;
height: 50px;
width: 50px;
/*display: none;*/
}
#flip,
#slide {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#slide {
padding: 50px;
display: none;
}
p {
margin-top: 20px;
padding: 5px;
border: 2px solid #666;
}
</style>
<script src="jquery/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("#first").animate({
left: '250px'
});
$("#second").animate({
left: '200px'
});
$("#third").animate({
left: '150px'
});
$("#fourth").animate({
left: '100px'
});
});
});
$(document).ready(function() {
$("#flip").click(function() {
$("#slide").slideToggle("slow");
});
});
$(function() {
$("div").click(function() {
var color = $(this).css("background-color");
$("p").html(color);
});
});
</script>
</head>
<body>
<div id="first" style="position: relative;">
</div>
<div id="second" style="position: relative;">
</div>
<div id="third" style="position: relative;">
</div>
<div id="fourth" style="position: relative;">
</div>
<p> </p>
<button type="button">Event</button>
<div id="flip">Click To Slide down</div>
<div id="slide">Welcome To Slide</div>
</body>
</html>```