javascript - 如何使用 jquery 在加载页面上的每个 div 下方显示颜色?
问题描述
我已经创建了 4 个 div 并为每个 div 赋予了颜色,现在我希望每个 div 下方显示每个的颜色,我为每个 div 设置了十六进制值,但它仅在我单击它时显示,我想要每个 div 下方的每个 div 的颜色在加载页面时。请帮忙。
请仅使用 jquery 帮助我解决问题。
$(function() {
$("div").click(function() {
var color = $(this).css("background-color");
$("#rgbresult").html(color);
var hex = rgb2hex(color);
$('#hexaresult').html(hex);
});
});
function rgb2hex(orig) {
var rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+)/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3], 10).toString(16)).slice(-2) : orig;
}
#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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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 id="rgbresult"> </p>
<p id="hexaresult"> </p>
解决方案
- 使用 class 并将每个结果 div 放在每个 div 旁边,然后使用 .next() 将相应的 div 结果指向此上下文
$("div").click(function() {
var color = $(this).css("background-color");
$(this).next(".rgbresult").html(color);
var hex = rgb2hex(color);
$(this).next().next(".hexaresult").html(hex);
});
$("div").each(function() {
var color = $(this).css("background-color");
$(this).next(".rgbresult").html(color);
var hex = rgb2hex(color);
$(this).next().next(".hexaresult").html(hex);
})
function rgb2hex(orig) {
var rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+)/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3], 10).toString(16)).slice(-2) : orig;
}
#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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="first" style="position: relative;">
</div>
<p class="rgbresult"> </p>
<p class="hexaresult"> </p>
<div id="second" style="position: relative;">
</div>
<p class="rgbresult"> </p>
<p class="hexaresult"> </p>
<div id="third" style="position: relative;">
</div>
<p class="rgbresult"> </p>
<p class="hexaresult"> </p>
<div id="fourth" style="position: relative;">
</div>
<p class="rgbresult"> </p>
<p class="hexaresult"> </p>
推荐阅读
- swiftui - 如何在 ForEach 中转换不同的 SwiftUI View 结构来显示?
- loops - ffmpeg 复杂过滤器 pip 循环不起作用
- typescript - 查找表的正确键入
- grafana - Grafana 的新手,这种图表的图表/插件是什么?
- php - 如何格式化 PostgreSQL 查询结果以使用 PHP 从关联数组中提取 GeoJSON 对象?
- haskell - 在 Haskell 中创建类型类但出现“歧义发生”错误
- r - 如何在 R 中正确运行 levene 测试?
- javascript - 无法在 reactNative 中使用 `npm install @react-navigation/stack` 安装“@react-navigation/stack”
- java - 如何在 ui.html 文件中使用 gwt 提供更多空间
- flutter - 从字节文件创建 Zip 文件 Flutter web