1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【Vue实战】使用vue实现点击按钮 改变被点击按钮的样式。

【Vue实战】使用vue实现点击按钮 改变被点击按钮的样式。

时间:2020-11-08 09:03:12

相关推荐

【Vue实战】使用vue实现点击按钮 改变被点击按钮的样式。

1. 实现思路

为每个按钮设置一个index;为按钮设置点击事件获取到按钮点击事件以后,判断index,进而动态修改按钮的class

2. 代码实现

2.1 代码

<template><div class="btnGroup"><buttonv-for="(item, index) in listData":key="index"@click="changeColor(index)":class="activeIndex === index ? 'active' : ''">{{item }}</button></div></template><script>export default {data() {return {listData: ["Button1", "Button2", "Button3", "Button4", "Button5"],// 标记被选中的按钮的indexactiveIndex: null,};},methods: {changeColor(index) {this.activeIndex = index;}}};</script><style lang="less" scoped>.btnGroup {width: 500px;height: 100px;button {width: 15%;height: 35%;margin: 20px 0 20px 20px;background-color: pink;border: 1px solid skyblue;}// 被选中的按钮的样式.active {background-color: antiquewhite;}}</style>

2.2 实现效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。