--categories:
- vue基础
tags: - vue事件绑定
目录
v-on事件绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-on事件绑定</title>
<style>
.base{
100px;
height: 100px;
border: 1px solid #0033aa;
}
.active{
background: #00ccbb;
}
.active2{
background: #00ccbb;
}
</style>
</head>
<body>
<div id="app">
<div>v-on 实现元素值自增</div>
<!-- v-on 实现元素值自增 -->
<div>{{ num1 }}</div>
<button v-on:click="num1 += 1">点击自增1</button>
<hr>
<div>v-on 实现元素值自增</div>
<!-- v-on 实现元素值自增 -->
<div>{{ num2 }}</div>
<button v-on:click="handleClick">点击自增1</button>
<hr>
<div>v-on 实现元素值自增</div>
<!-- v-on 实现元素值自增 -->
<!-- :class="{类名:状态值}" -->
<div class="base" :class="{active:active}">主块</div>
<button v-on:click="changeClick">点击切换颜色</button>
<hr>
<div>v-on 实现元素值自增</div>
<!-- v-on 实现元素值自增 -->
<!-- :class="{类名:状态值}" -->
<div class="base" :class="{active2:active2}">主块</div>
<button @click="changeClick2">点击切换颜色</button>
<hr>
</div>
<!-- 1. 引包-->
<script src="./vue.js"></script>
<script>
// 2.初始化
const vm = new Vue({
el: '#app',
// 数据属性
data: {
num1: 0,
num2: 0,
active: false,
active2: false
},
methods:{
handleClick(){
this.num2 +=1;
},
changeClick(){
this.active = !this.active;
},
changeClick2(){
this.active2 = !this.active2;
}
}
})
</script>
</body>
</html>