选项卡1:使用 CSS 类来控制内容的显示和隐藏
<!DOCTYPE html>
<html>
<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>选项卡1</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
</head>
<style>
.box {
width: 400px;
height: 300px;
color: #fff;
}
.option button {
background-color: #faebd7;
border: none;
width: 100px;
height: 50px;
}
.option .active {
background-color: blueviolet;
}
.content {
height: 300px;
background-color: blue;
font-size: 30px;
}
.content div {
/* 将所有内容隐藏 */
display: none;
}
.content .con {
/* 将内容显示出来 */
display: block;
}
</style>
<body>
<li>使用 CSS 类来控制内容的显示和隐藏</li>
<div id='app'>
<div class="box">
<!-- 上面按钮 -->
<div class="option">
<button :class="{active:num==1}" @click="num=1">按钮一</button>
<button :class="{active:num==2}" @click="num=2">按钮二</button>
<button :class="{active:num==3}" @click="num=3">按钮三</button>
</div>
<!-- 下面展示内容 -->
<div class="content">
<div :class="{con:num==1}">汉堡</div>
<div :class="{con:num==2}">薯条</div>
<div :class="{con:num==3}">炸鸡</div>
</div>
</div>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
num: 1
}
})
</script>
</html>
选项卡2:使用Vue的 v-show 指令来实现内容的切换
<!DOCTYPE html>
<html>
<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>选项卡2</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
</head>
<style>
.box {
width: 400px;
height: 300px;
color: #fff;
}
.option button {
background-color: #faebd7;
border: none;
width: 100px;
height: 50px;
}
.option .active {
background-color: blueviolet;
}
.content {
height: 300px;
background-color: blue;
font-size: 30px;
}
</style>
<body>
<li>使用Vue的 v-show 指令来实现内容的切换</li>
<div id='app'>
<div class="box">
<!-- 上面按钮 -->
<div class="option">
<button :class="{active:num==1}" @click="num=1">按钮一</button>
<button :class="{active:num==2}" @click="num=2">按钮二</button>
<button :class="{active:num==3}" @click="num=3">按钮三</button>
</div>
<!-- 下面展示内容 -->
<div class="content">
<div v-show="num==1">汉堡</div>
<div v-show="num==2">薯条</div>
<div v-show="num==3">炸鸡</div>
</div>
</div>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
num: 1
}
})
</script>
</html>