动态添加类名Class

1,微信小程序
 

 <view class="lastTime {{learinfrom.IsLike == 0 ? 'selected' : ''}}"  style="font-size: 24rpx;color:#808080;"
>点赞(大拇指)</view>

2,h5
 

<view  :class="goodsListType == 'all'?'typeAct':''"  @click='getAll'>全部</view>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中动态添加class类名可以通过以下方式实现: 1. 使用对象语法: 你可以使用一个对象来指定需要添加class类名,对象的key为类名,value为一个布尔值,如果该值为真,则添加对应的类名,如果为假,则不添加对应的类名。 ```html <template> <div :class="{ 'active': isActive, 'highlight': isHighlighted }"></div> </template> <script> export default { data() { return { isActive: true, isHighlighted: false }; } }; </script> <style> .active { color: red; } .highlight { background-color: yellow; } </style> ``` 在上面的例子中,当isActive为true时,div元素会添加active类名,从而应用red颜色样式;当isHighlighted为false时,div元素不会添加highlight类名,从而不应用yellow背景色样式。 2. 使用数组语法: 你可以使用一个数组来指定需要添加class类名,数组中的每个元素都可以是一个字符串或一个动态绑定的数据。 ```html <template> <div :class="[activeClass, errorClass]"></div> </template> <script> export default { data() { return { activeClass: 'active', errorClass: 'error' }; } }; </script> <style> .active { color: red; } .error { border: 1px solid red; } </style> ``` 在上面的例子中,div元素会同时添加active和error类名,从而应用red颜色样式和红色边框样式。 3. 使用计算属性: 你可以利用计算属性来动态计算需要添加class类名。 ```html <template> <div :class="computedClass"></div> </template> <script> export default { data() { return { isActive: true, isHighlighted: false }; }, computed: { computedClass() { return { 'active': this.isActive, 'highlight': this.isHighlighted }; } } }; </script> <style> .active { color: red; } .highlight { background-color: yellow; } </style> ``` 在上面的例子中,计算属性computedClass会根据isActive和isHighlighted的值动态返回一个对象,然后该对象会被应用到div元素的class属性上。 以上是在Vue中动态添加class类名的几种常见方式,你可以根据具体场景选择合适的方式来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值