Html 如何用不同的颜色给css中的每个li上色
每个人 我有这个css颜色每一个李它的工作 您可以在本文中看到示例 这就是我最后要问的代码Html 如何用不同的颜色给css中的每个li上色,html,css,Html,Css,每个人 我有这个css颜色每一个李它的工作 您可以在本文中看到示例 这就是我最后要问的代码 .post ul li:nth-of-type(12){ background-color: #9FA4C4; } .post ul li:nth-of-type(13) { background: #B3CDD1 ; } .post ul li:nth-of-type(14) { background: #9E768F ; } .post ul li:nth-of-type(15)
.post ul li:nth-of-type(12){
background-color: #9FA4C4;
}
.post ul li:nth-of-type(13) {
background: #B3CDD1 ;
}
.post ul li:nth-of-type(14) {
background: #9E768F ;
}
.post ul li:nth-of-type(15) {
background: #9FA4C4 ;
}
.post ul li:nth-of-type(16) {
background: #B279A7 ;
}
.post ul li:nth-of-type(17) {
background: #D387AB ;
}
.post ul li:nth-of-type(18) {
background: #864BA2 ;
}
.post ul li:nth-of-type(21) {
background: #BF3A30 ;
}
.post ul li:nth-of-type(10) {
background: #6E45E1 ;
}
.post ul li:nth-of-type(11) {
background: #89D4CF ;
}
.post ul li:nth-of-type(2) {
background: #A7ACD9 ;
}
.post ul li:nth-of-type(3) {
background: #9E8FB2;
}
.post ul li:nth-of-type(4) {
background: #A88BEB ;
}
.post ul li:nth-of-type(5) {
background: #F8CEEC ;
}
.post ul li:nth-of-type(6) {
background: #B58ECC ;
}
.post ul li:nth-of-type(7) {
background: #5DE6DE ;
}
.post ul li:nth-of-type(8) {
background: #647DEE ;
}
.post ul li:nth-of-type(9) {
background: #7F53AC ;
}
.post ul li:nth-of-type(19) {
background: #77EED8 ;
}
.post ul li:nth-of-type(20) {
background: #9EABE4 ;
}
但我想使用循环或函数将其转换为动态
如果有想法,感谢您的帮助。您可以尝试以下方法:
// Fetch the list of elements matching the selector
const listElements = document.querySelectorAll('.post ul li');
// Store the colors in an array
const colors = [here you can store the colors you want]; // Assuming this has the same number of values as the list in your html, otherwise the below loop will fail if the values are less.
// Map each color with the corresponding list element
for(let index in listElements) {
listElements[index].style.backgroundColor = colors[index];
}
谢谢您的帮助,但不起作用。您可以共享您正在尝试的代码吗?const listElements=document.querySelectorAll('.post ul li');常量颜色=[“535b2d”、“23a6d5”、“2d335b”、“23a6d5”、“e73c7e”、“ffff0000”];对于(let index in listElements){listElements[index].style.backgroundColor=colors[index];}//注意,我添加了大约20种颜色,甚至不起作用,这些颜色比列表项还要多。请在编写此代码的地方共享完整文件。是否在脚本标记中添加此代码?我在没有脚本标记的head标记中添加此代码,然后添加脚本标记const listElements=document.querySelectorAll('.post ul li');这些颜色的颜色是:[以下以下几方面的颜色:以下几方面的颜色是:以下几方面的颜色是:以下几方面的颜色:以下几方面的颜色是:以下几方面的颜色:以下几方面的颜色:以下几方面的颜色是:以下几方面的颜色:以下几方面的颜色是:以下几方面的颜色是:以下几方面的颜色是:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“,”B279A7“,”9FA4C4“,”9E768F“,”B3CDD1“,”9FA4C4“];对于(在listElements中设置索引){listElements[index].style.backgroundColor=colors[index];}但不起作用