1.隐藏/显示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js v-on指令练习1</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- vue-app的根容器 -->
<div id="app">
<h2 v-if="show">{{name}}</h2>
<button type="button" @click="handleClick">隐藏/显示</button>
</div>
<script type="text/javascript">
// 实例化一个Vue对象
var app = new Vue({
el: '#app',
data:{
name:'软件1721',
show:true
},
methods:{
handleClick:function(){
this.show=!this.show;
}
}
})
</script>
</body>
</html>
2.年龄加减
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js v-on指令练习3--年龄的加减</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- vue-app的根容器 -->
<div id="app">
<h2>{{age}}</h2>
<button type="button" @click="add">加一岁</button>
<button type="button" @click="substrct(5)">减五岁</button>
</div>
<script type="text/javascript">
// 实例化一个Vue对象
var app = new Vue({
el: '#app',
data:{
age:30
},
methods:{
add:function () {
this.age +=1;
},
substrct:function (num) {
this.age -=num;
if(this.age - num<0){
alert('太小了');
}else{
this.age-=0;
}
}
}
}
)
</script>
</body>
</html>
关注/取关
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js v-on指令练习3--结合图标的关注/取关练习</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css" />
<style type="text/css">
.followed {
color: #ddd;
}
.cancle-followed{
color:greenyellow;
}
.link {
cursor: pointer;
}
</style>
</head>
<body>
<!-- vue-app的根容器 -->
<div id="app">
<h2>{{name}}</h2>
<span class="followed link" v-show="followed" @click="handleFollow">
<i class="icon-ok"></i>已关注
</span>
<span class="cancle-followed link" v-show="followed===false" @click="handleFollow">
<i class="icon-plus"></i>关注
</span>
</div>
<script type="text/javascript">
// 实例化一个Vue对象
var app = new Vue({
el: '#app',
data: {
name: '简书作者',
followed: false
},
methods: {
handleFollow:function(){
this.followed= !this.followed;
}
},
})
</script>
</body>
</html>
实战练习:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js v-on指令练习3--结合图标的关注/取关练习</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css" />
<style type="text/css">
.followed {
color: mediumvioletred;
}
.cancle-followed {
color: red;
}
.link {
cursor: pointer;
}
.btn {
width: 100px;
height: 40px;
color: rgb(234, 111, 90);
border-radius: 20px;
font-size: 14px;
background-color: #FFFFFF;
border: 1px solid rgb(234, 111, 90);
outline: none;
}
.btn1{
width: 100px;
height: 40px;
color: rgb(234, 111, 90);
border-radius: 20px;
font-size: 14px;
background-color: #FFFFFF;
border: 1px solid rgb(234, 111, 90);
outline: none;
}
</style>
</head>
<body>
<div id="app">
<h2>{{name}}</h2>
<button type="button" class="btn" v-show="like" @click="iffollow"><i class="icon-heart-empty"></i>喜欢 |{{likes}}
</button>
<button type="button" class="btn1" v-show="like===false" @click="unfollow(1)"><i class="icon-heart"></i>喜欢 |{{likes}}
</button>
</div>
<script type="text/javascript">
// 实例化一个vue对象
var app = new Vue({
el: '#app',
data: {
name: '简书作者',
followed: false,
likes: 1000,
like: true
},
methods: {
handlefollow: function() {
this.followed = !this.followed;
},
iffollow: function() {
this.like = !this.like;
if (like = true) {
this.likes += 1;
}
},
unfollow: function(num) {
this.like = !this.like;
this.likes -= num;
}
}
})
</script>
</body>
</html>