-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo05.html
102 lines (90 loc) · 1.98 KB
/
demo05.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html>
<head>
<title>条件渲染</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<!-- v-if v-else 条件语句 -->
<div id="app-1">
<h1 v-if="ok">yes</h1>
<h1 v-else>no</h1>
</div>
<!-- 在<template>元素上使用v-if条件渲染分组 -->
<div id="app-2">
<template v-if="ok">
<h1>Title</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</template>
</div>
<div id="app-3">
<div v-if="Math.random() > 0.5">
Now you see me
</div>
<div v-else>
Now you don't
</div>
</div>
<!-- 用key管理可以复用的元素 -->
<!-- 点击按钮不会改变input里已经输入的值,而是改变的placeholder,共用了一个input -->
<div id="app-4">
<template v-if="loginType === 'username'">
<label>Username</label>
<input placeholder="Enter your username">
</template>
<template v-else>
<label>Email</label>
<input placeholder="Enter your email">
</template>
<button v-on:click="toggleLoginType">Toggle login type</button>
</div>
<div id="app-5">
<template v-if="loginType === 'username'">
<label>Username</label>
<input placeholder="Enter your username" key="username-input">
</template>
<template v-else>
<label>Email</label>
<input placeholder="Enter your email" key="email-input">
</template>
<button v-on:click="toggleLoginType">Toggle login type</button>
</div>
<!--
根据条件展示元素的选项v-show,但是带有v-show的元素始终被渲染并保留在DOM中,
只是简单的切换元素CSS属性display -->
<h1 id="hh1" v-show="ok">Hello!</h1>
<script>
var vm=new Vue({
el:'#app-1',
data:{
ok:false
}
})
var app3=new Vue({
el:'#app-3'
})
var app4=new Vue({
el:'#app-5',
data:{
loginType:'username'
},
methods:{
toggleLoginType:function(){
if(this.loginType==='username'){
this.loginType=''
}else{
this.loginType='username'
}
}
}
})
var hh=new Vue({
el:'#hh1',
data:{
ok:false
}
})
</script>
</body>
</html>