内部网站建设依据文件,wordpress卡车主题,半导体网站建设,针对网站开发软件 代替手动介绍
在vue的 for 循环中#xff0c;经常会使用到动态添加类名或者样式的情况#xff0c;实现给当前的选中的 div 添加不同的样式。
动态添加类名
提示#xff1a; 所有动态添加的类名#xff0c;放在表达式里都需要添加引号#xff0c;进行包裹。
通过 对象 的形式经常会使用到动态添加类名或者样式的情况实现给当前的选中的 div 添加不同的样式。
动态添加类名
提示 所有动态添加的类名放在表达式里都需要添加引号进行包裹。
通过 对象 的形式使用花括号进行包裹。 使用方法{类名 boolean} 第一个参数是需要添加的类名第二个参数是一个 boolean值。 优点是 可以通过逗号进行分割即可添加多个类名 代码如下
templatediv classindexdiv v-for(item,index) in state.list :keyindex classlist-box!-- 通过list数组里isActive属性给p元素添加active类名--p :class{active: item.isActive{{ item.title }}/p!-- 给p元素添加active类名通过list数组里isRed属性给p元素添加red类名这里添加了两个类名通过逗号进行分隔的--p :class{active: item.isActive, red: item.isRed}{{ item.title }}/p/divdiv/div/div
/template
script setup
import {reactive} from vue;
const state reactive({list: [{id: 0,title: 星期一,isActive: true,isRed: false,},{id: 1,title: 星期二,isActive: false,isRed: true,}]
})
/script
style langless scoped.list-box {padding: 0 20px;p {margin-top: 20px;}.active {font-size: 20px;}.red {color: red;}}
/style实现效果如下
通过 数组 的形式使用 []进行包裹。 使用方法 使用方法[判断成立的情况 ? 类名1 类名2] 第一个参数是判断条件成立的表达式第二个参数是表达式成立的情况下添加的类名第三个表达式是情况不成立的情况下的类名。 代码如下
templatediv classindexdiv v-for(item,index) in state.list :keyindex classlist-box!-- 通过list数组里isRed属性为true时给p元素添加red类名的样式否则不添加这里添加了两个类名通过逗号进行分隔的--p :class[item.isRed ? red : ]{{ item.title }}/p!-- 三元同时添加多个类名使用空格进行隔开就行 如下所示 --p :class[item.isRed ? red blue : ]{{ item.title }}/p/divdiv/div/div
/template
script setup
import {reactive} from vue;
const state reactive({list: [{id: 0,title: 星期一,isRed: false,},{id: 1,title: 星期二,isRed: true,}]
})
/script
style langless scoped.list-box {padding: 0 20px;p {margin-top: 20px;}.red {color: red;}}
/style实现效果如下
通过 方法 的形式在方法当中返回想要的类名。 使用方法methods(),直接写方法名。 提示逻辑多的时候建议用方法来添加类名只有一个判断的时候建议用三元。 代码如下
templatediv classindexdiv v-for(item,index) in state.list :keyindex classlist-box!-- 直接返回方法名例如这里我的方法是 isred --p :classisRed(item){{ item.title }}/p/divdiv/div/div
/template
script setup
import {reactive} from vue;
const state reactive({list: [{id: 0,title: 星期一,isRed: false,},{id: 1,title: 星期二,isRed: true,}]
})
// 在方法里写自己的判断然后返回对应的类名
const isRed (item) {return item.isRed ? red :
}
/script
style langless scoped.list-box {padding: 0 20px;p {margin-top: 20px;}.red {color: red;}}
/style实现效果如下
动态添加style样式
提示 在vue中动态添加 style 样式
所有的样式名必须是 驼峰写法比如 font-size必须写成 fontSize;除了绑定的值以外属性都得用引号括起来比如 fontSize: 12px
通过对象 的形式 代码如下
templatediv classindex!-- 页面直接添加样式 --div :style{fontSize: 20px, color: red}哈哈哈/div!-- vue 动态添加样式通过数据的双向绑定 --div :style{fontSize: state.activeSize px, color: state.activeColor}嘿嘿嘿/div/div
/template
script setup
import {reactive} from vue;
const state reactive({activeSize: 14,activeColor: blue
})
/script效果如下
通过数组 的形式 代码如下
templatediv classindexdiv :style[state.sizeStyle,state.colorStyle]哈哈哈/div/div/div
/template
script setup
import {reactive} from vue;
const state reactive({sizeStyle: {fontSize: 18px,height: 40px,lineHeight: 40px,width: 80px,textAlign: center},colorStyle: {color: red,border: 1px solid green}
})
/script效果如下
通过 三元判断的形式进行添加 代码如下
templatediv classindexdiv :stylestate.active ? color: red: 哈哈哈/div/div
/template
script setup
import {reactive} from vue;
const state reactive({active: true,
})
/script
style
.active {color: red;
}
/style效果如下所示 4. 通过 方法的形式进行返回。 代码如下
templatediv classindexdiv :stylesetStyle()哈哈哈/div/div
/template
script setup
const setStyle () {return background: red; height: 40px;line-height: 40px; width: 70px;color: #fff; text-align: center;
}
/script效果如下
总结
这里我用的是 vue3的一些写法在vue2里用法一样在data里定义数据即可。 文章转载自: http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.snmth.cn.gov.cn.snmth.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.krgjc.cn.gov.cn.krgjc.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.qrqdr.cn.gov.cn.qrqdr.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn