这篇文章主要介绍了 vue 动态修改 a 标签的样式的方法, 小编觉得挺不错的, 现在分享给大家, 也给大家做个参考一起跟随小编过来看看吧
公司要做一个类似今日头条的项目, 用前后端分离的方式做成 html5 页面, 先在微信中运行, 领导说想看看效果怎么样今日头条头部的导航是可以滚动和添加类别的, 我们这个项目也是一样所以在导航这个地方就需要在点击不同分类的同时, 样式也是跟着变的, 我在网上搜索了一下, 发现了下面的代码, 简洁清淅, 不过具体网址忘记了, 先把代码贴出来给大家看一下, 想知道网址的可以去网上搜索一下
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/CSS">
- .active{
- color: red;
- }
- </style>
- </head>
- <body>
- <div id="app">
- <button v-for='item in isp' @click='f1($index)'
- :class="{'active': $index === isActive}">{{item}}</button>
- </div>
- <script type="text/javascript" src='vue.js'></script>
- <script>
- new Vue({
- el:'#app',
- data:{
- isActive:0,
- isp: ['BGP','中国电信','中国联通','联通电信双线']
- },
- methods:{
- f1:function(index){
- this.isActive=index
- }
- }
- })
- </script>
- </body>
- </html>
像下面这样:
vue 动态修改 a 标签的样式. jpeg
下面是我项目中的代码:
上面的代码, 作者用的是 vue 1.0 版本, 下面我用的是 2.0 版本
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>
- Document
- </title>
- <style type="text/css">
- .red-link{ color: red; }
- </style>
- </head>
- <body>
- <div id="app">
- <a href="javascript:void (0);" rel="external nofollow" class="box1-item"
- v-for="(item, index) in menu" :class="{'red-link':index === isActive }"
- v-on:click.stop.prevent='switchTab(index)'>
- {{ item }}
- </a>
- </div>
- <script type="text/javascript" src='vue.js'>
- </script>
- <script>
- new Vue({
- el: '#app',
- data: {
- isActive: 0,
- menu: ['推荐', '人物', '干货', '行业', '融资', '教育', '大数据'],
- },
- methods: {
- switchTab: function(index) {
- this.isActive = index;
- }
- }
- })
- </script>
- </body>
- </html>
代码基本一样, 只是自己整理一下, 方便下次使用的时候好找, 也希望需要的朋友能拿来就用
来源: http://www.phperz.com/article/18/0215/362924.html