消息弹框
语法糖:v-on:click 简写成@click
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button type="button" @click="handleClick">点我</button>
<!-- <button type="button" v-on:click="handleClick">点我</button> -->
</div>
<script type="text/javascript">
var app = new Vue({
el:'#app',
data:{
name:'软件1721',
},
methods:{
handleClick:function(){
alert(this.name);
}
}
})
</script>
</body>
</html>
关注取消
应用v-if 当前值的取反(注释掉的部分易于理解)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>隐藏和显示切换练习</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h2 v-if="show">{{name}}</h2>
<button type="button" @click="handleClick">隐藏/显示</button>
<!-- <button type="button" v-on:click="handleClick">点我</button> -->
</div>
<script type="text/javascript">
var app = new Vue({
el:'#app',
data:{
name:'软件1721',
show:true,
},
methods:{
handleClick:function(){
//把当前值取反
/* if(this.show === true){
this.show = false;
}else{
this.show = true;
} */
this.show = !this.show;
}
}
})
</script>
</body>
</html>
年龄增减练习
methods中写方法,在按钮中调用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>年龄加减</title>
<!-- 通过CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style type="text/css">
</style>
<body>
<div id="app">
<h2 v-if="true">{{age}}</h2>
<button type="button" @click="handclick">加一岁</button>
<button type="button" @click="dissclick">减五岁</button>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
age: 17
},
methods: {
handclick: function() {
this.age += 1
},
dissclick: function() {
if (this.age - 5 > -1) {
this.age -= 5
} else {
alert("年龄不能小于0")
}
}
}
})
</script>
</body>
</html>
关注与取消关注
关注与取消关注练习,是频繁的操作,所以要采用v-show
(需要css font 引入图标)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>关注取消练习</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">
.follwed{
color: #666666;
}
.cancle{
color: #008000;
}
.link{
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<h2>{{name}}</h2>
<span class="follwed link" v-show="followed" @click="handclick">
<i class="icon-ok"></i>已关注
</span>
<span class="cancle link" v-show="followed===false" @click="handclick">
<i class="icon-plus"></i>关注
</span>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
name:'樱花树下的约定',
followed:true
},
methods: {
handclick:function(){
this.followed=!this.followed
}
},
}
)
</script>
</body>
</html>
网友评论