vue.js-动态绑定class

时间:2023-01-29 22:46:03

基于vue.js 1.X版本
点击按钮,动态绑定class。

<!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.js-动态绑定class