1、使用index
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
< http-equiv="X-UA-Compatible" content="ie=edge">
< >vue导航</ >
<style media="screen">
*{
margin:0;
padding: 0;
}
ul li{
list-style: none;
}
.navul{
margin:100px auto 20px;
overflow: hidden;
}
.navul li{
background-color: #5597b4;
padding:18px 30px;
float:left;
color: #fff;
font-size: 18px;
cursor: pointer;
}
.active{
background-color: #5597b4;
}
.home .home,.new .new,.contact .contact,.service .service{
background-color: skyblue;
}
.checked{
background: #eff4f7;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li v-for="(relation,index) in relations" v-bind:id="relation.id" v-bind:id="relation.id" v-bind:class="{checked:index==nowIndex}" v-on:click="relati (index)">
<i></i>
<span class="">{{relation.text}}</span>
</li>
</ul>
</div>
< src="js/vue.js" charset="utf-8"></ >
< type="text/ ">
var nav = new Vue({
el:'#nav',
data:{
relations: [
{text:'项目',id:'program'},
{text:'人员',id:'person'},
{text:'机构',id:'organization'},
{text:'技术',id:'tech'},
{text:'地区',id:'location'},
{text:'国家',id:'country'}
],
nowIndex:0
// willShow:false
},
methods:{
relati :function(index){
this.nowIndex=index;
}
}
});
</ >
</body>
</html>
2、通过改变当前值切换类名
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
< http-equiv="X-UA-Compatible" content="ie=edge">
< >点击切换</ >
<style>
.glyphicon-minus{
color:#08f;
}
.glyphicon-plus{
color:purple;
}
</style>
< src="js/vue.js"></ >
</head>
<body>
<div id="box">
<div style="width:100px; height:100px; background: #6bacdf;" v-show="isDisplay"></div>
<div class="form-group">
<button class="btn"
:class="[isShow?'btn-primary':'']"
@mouseover="toggleShow()"
@mouseout="toggleShow()"
v-text="btnStyle"></button>
</div>
<div class="">
<button class="btn"
:class="[isTrue?'glyphicon-minus':'glyphicon-plus']"
@click="toggleBar()">切换类名按钮</button>
</div>
</div>
< type="text/ ">
new Vue({
el:'#box',
data:{
isDisplay:false,
isShow: false,
isTrue:true,
btnStyle: 'Default'
},
methods:{
toggleShow: function() {
this.isShow = !this.isShow;
},
toggleBar:function(){
this.isTrue = !this.isTrue;
}
},
watch: {
isShow: function(newVal) {
// console.log(newVal);
newVal ? this.btnStyle = 'Primary' : this.btnStyle = 'Default'
}
},
});
</ >
</body>
</html>
拓展知识:vue点击改变样式的实例
data里
isActive:-1,
method里
checkedItem(index){
this.isActive=index;
},
页面里
<div v-for="(item,index) in nameoptions" v-bind:class="{active:index==isActive}" @click="checkItem(index)>{{item.name}}</div>
以上这篇Vue2.0点击切换类名改变样式的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
2017年度云栖大会重磅干货集锦
下一篇 :
一个阿里产品经理眼中的“垃圾分类”
-
企业级业务架构设计方法与“中台”概念的比较
2026-05-18栏目: 教程
-
为什么Flink会成为下一代大数据处理框架的标准?
2026-05-18栏目: 教程
-
微服务架构:从事务脚本到领域模型
2026-05-18栏目: 教程
-
微服务架构到底是什么鬼?
2026-05-18栏目: 教程
-
Linux系统安全之Rootkit原理解析与检测实践
2026-05-18栏目: 教程
