代碼演示如下:(寫的不好的地方,請見諒)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.yangshi1 {
background-color: aqua;
width: 100px;
height: 100px;
}
.yangshi2 {
background-color: rebeccapurple;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="div">
<button @click="qiehuan">切換</button>//給一個點擊事件
<div v-bind:class="{ yangshi1: ys1 ,yangshi2: ys2}"></div>//這里是兩個class,這里看不懂的話可以去看看Vue的官網的學習教程,有很詳細的介紹
</div>
<script src="./js/vue.js"></script>
<script>
var dianji = new Vue({
el: "#div",
data: {
ys1: true,
ys2: false
},//記得一定要用 “ , ”隔開,不然會報錯的
methods: {
//方法
qiehuan() {
//當點擊第一次時,ys1從true變為false,Style樣式yangshi1就會隱藏
//再點擊第二次的時候由false,變為true,顯示樣式
this.ys1 = !this.ys1
//代碼運行到下面,值為false的時候,取反,就會為true,Style樣式yangshi2就會顯示
//這里到第二次點擊的時候,就由true變為false,隱藏樣式
this.ys2 = !this.ys2 //這里 “ !” 是取反
}
}
})
</script>
</body>
</html>
