当前位置:网站首页>Dynamically changing the style of label elements in a loop

Dynamically changing the style of label elements in a loop

2022-06-22 18:56:00 Whirling and fluttering

Utilization cycle v-for Place tag elements on the page

<div v-for=(item,index) in list :key="index">{
   {item}}/div>
list=[
'red','yellow','green','blue'
]

Set different class styles

.red{
    color:red}
.yellow{
    color:yellow}
.green{
    color:green}
.blue{
    color:blue}

Define objects to bind indexes ( Or other items , It needs to be decided ) Relationship with class style

const colorEnum={
    
red:{
    class:'red'},
yellow:{
    class:'yellow'},
green:{
    class:'green'},
blue:{
    class:'blue'},
}

Modify the class style in the label

<div v-for=(item,index) in list :key="index" :class="$$(colorEnum, [item], 'class')" >{
   {item}}/div>

Effect view

 Insert picture description here

Code instructions

explain : The code is written in vue In the frame
Key sentences :class="$$(colorEnum, [item], 'class')" // Add two. $ To dynamically match item and colorEnum Key in the corresponding object in class Value .

原网站

版权声明
本文为[Whirling and fluttering]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221717588780.html