Vue实现根据hash高亮选项卡
时间:2022-04-23 08:45:44|栏目:vue|点击: 次
本文实例为大家分享了Vue实现根据hash高亮选项卡的具体代码,供大家参考,具体内容如下
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> a { color: #555; } .active { color: red; } </style> </head> <body> <main id="box"> <div class="tab"> <a v-for="tab in tabs" :href="tab.href" :class="{active:tab.href==myhash}">{{tab.title}}</a> </div> </main> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script> var vm=new Vue({ el:'#box', data:{ tabs: [ { title: '所有任务', href: '#all' }, { title: '未完成任务', href: '#unfinished' }, { title: '完成的任务', href: '#finished' } ], myhash:'#all' //存储当前hash值 } }); function watchHashChange(){ var hash = window.location.hash; if(hash!=''){ vm.myhash = hash; //将hash值传过去 }else{ vm.myhash = '#all'; //否则用默认值 } } watchHashChange(); window.addEventListener('hashchange',watchHashChange); </script> </body> </html>