简体   繁体   English

根据 CSS class 名称设置背景颜色

[英]Set background color based on a CSS class name

I have these different div elements with classes such as red or green .我有这些不同的 div 元素,它们具有诸如redgreen之类的类。

<div class="sidebar">
    <div class="color_box red"></div>
    <div class="color_box orange"></div>
    <div class="color_box yellow"></div>
    <div class="color_box green"></div>
    <div class="color_box blue"></div>
    <div class="color_box purple"></div>
    <div class="color_box black"></div>
    <div class="color_box white"></div>
</div>

I want to set the background color of the div element to its class name.我想将 div 元素的背景颜色设置为其 class 名称。 So if the class name is red , I would want the div background to be red.因此,如果 class 名称为red ,我希望 div 背景为红色。 I'm not sure if I need to add javascript or something else.我不确定是否需要添加 javascript 或其他内容。 I've looked at this and it was not what I wanted at all.我看过这个,这根本不是我想要的。

Thank you in advance!先感谢您!

A solution that is dynamic and does not depend on class names is the use of a custom data attribute .一个不依赖于 class 名称的动态解决方案是使用自定义数据属性

Take a look at the following solution:看看下面的解决方案:

 <div class="sidebar"> <div data-backgroundcolor="red" class="color_box">BOX</div> <div data-backgroundcolor="orange" class="color_box">BOX</div> <div data-backgroundcolor="yellow" class="color_box">BOX</div> <div data-backgroundcolor="green" class="color_box">BOX</div> <div data-backgroundcolor="blue" class="color_box">BOX</div> <div data-backgroundcolor="purple" class="color_box">BOX</div> <div data-backgroundcolor="black" class="color_box">BOX</div> <div data-backgroundcolor="white" class="color_box">BOX</div> </div> <script> const colorboxes = document.querySelectorAll('.color_box'); colorboxes.forEach(el => { el.style.backgroundColor = el.dataset.backgroundcolor }) </script>

An alternative way to approach this without JavaScript if you're using the SASS pre-processor would be to use an @each rule.如果您使用的是 SASS 预处理器,那么在没有 JavaScript 的情况下解决此问题的另一种方法是使用@each规则。

$colors: red, orange, yellow, green, blue, purple, black, white;

@each $color in $colors {
    .bg-color-#{$color} {
        background-color: $color;
    }
}

Demo演示

The solution below will get the color name in the class and set it to background color.下面的解决方案将获取 class 中的颜色名称并将其设置为背景颜色。

REMEMBER This specific solution will only works if the color class is the second class in the list.记住,此特定解决方案仅在颜色 class 是列表中的第二个 class 时才有效。

var background = $(this).attr("class").split(" ")[1]; will give second class name in class names list.将在 class 名称列表中给出第二个 class 名称。

For example:例如:

$(this).attr("class").split(" ")[0] is color_box $(this).attr("class").split(" ")[0]color_box

and

$(this).attr("class").split(" ")[1] is red $(this).attr("class").split(" ")[1]red

 $(document).ready(function(){ $(".color_box").each(function(){ var background = $(this).attr("class").split(" ")[1]; $(this).css("background-color",background); }); });
 .color_box { display: block; width: 100%; height: 100px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="sidebar"> <div class="color_box red"></div> <div class="color_box orange"></div> <div class="color_box yellow"></div> <div class="color_box green"></div> <div class="color_box blue"></div> <div class="color_box purple"></div> <div class="color_box black"></div> <div class="color_box white"></div> </div>

  1. Get all .color_box -elements获取所有.color_box元素
  2. For-each:对于每个:
    1. Find class-name, that is not color_box查找类名,即不是color_box
    2. Set background-color to found class-name将背景颜色设置为找到的类名

No need for jQuery or any other library!不需要 jQuery 或任何其他库! It's all native JavaScript!都是原生 JavaScript!

 const colorboxes = document.querySelectorAll('.color_box'); for (let cb of colorboxes) { cb.style.backgroundColor = Object.values(cb.classList).find(e => e;= 'color_box'); }
 div.color_box { width: 20px; height: 20px; }
 <div class="sidebar"> <div class="color_box red"></div> <div class="color_box orange"></div> <div class="color_box yellow"></div> <div class="color_box green"></div> <div class="color_box blue"></div> <div class="color_box purple"></div> <div class="color_box black"></div> <div class="color_box white"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM