在vue中使用事件来切换绑定的class样式,在vue-cli脚手架中的Home.vue中
<template> <div id="main"> <li v-for="v in news"> <span :class="v.status ? ‘success‘ : ‘error del‘">{{v.title}}</span> <button @click="switchState(v,true)" v-if="!v.status">恢复</button> <button @click="switchState(v,false)" v-if="v.status">删除</button> </li> </div> </template> <script> export default{ data(){ return { news:[ {title:‘rock‘,status:true}, {title:‘cena‘,status:true}, {title:‘randy‘,status:true} ] }; }, methods:{ switchState(v,status){ v.status = status; } } } </script> <style> .success{ color:green; } .error{ color:red; } .del{ text-decoration:line-through; } </style>
效果如下:
原文地址:https://www.cnblogs.com/wntd/p/9334801.html
时间: 2024-10-05 21:39:19