<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> a { color: #555; } .active { color: red; } </style> </head> <body> <main> <div> <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>
Vue实现根据hash高亮选项卡
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/e9957ad10bd3e297b6cca9e58155cd8e.html