实验1:个人信息填写
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<style>
p{
width:80%;
margin:0 auto;
}
input,
select{
display:inline-block;
width:50%;
height:25px;
}
select{
height:40px;
}
button{
width:100px;
background:rgb(41;135;243);
color:white;
}
.err_content li{
color:red;
}
</style>
</head>
<body>
<form id="my_form" @submit="checkForm">
<div class="err_content" v-if="errMsg.length">
<b>错误提示:</b>
<ul>
<li v-for="err in errMsg">{{err}}</li>
</ul>
</div>
<p>
<label for="user_name">姓名:</label>
<input id="user_name" name="user_name" type="text" v-model="user_name"></input>
</p>
<br>
<p>
<label for="user_age">年龄:</label>
<input id="user_age" name="user_age" type="number" v-model="user_age" min="0" max="100">
</p>
<br>
<p>
<label for="user_like">爱好:</label>
<select name="user_like" id="user_like" v-model="user_like">
<option value="0">看书</option>
<option value="1">写代码</option>
<option value="2">看代码</option>
</select>
</p>
<br>
<p>
<button type="submit">提交</button>
</p>
</form>
<script type="text/javascript">
new Vue({
el:'#my_form',
data:function(){
return{
errMsg:[],
user_name:null,
user_age:null,
user_like:null
}
},
methods:{
checkForm(e){
this.errMsg=[]
if(!this.user_name){
this.errMsg.push('用户姓名不能为空')
}
if(!this.user_age){
this.errMsg.push('年龄格式输入错误')
}
if(!this.user_like){
this.errMsg.push('用户爱好不能为空')
}
if(!this.errMsg.length)
return true
e.preventDefault()
}
}
});
</script>
</body>
</html>
实验结果
实验2:温度转华氏度
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div>
请输入摄氏温度:<input v-model="ctemp">
<br>
<br>
华氏温度:<child v-bind:temp="ctemp"></child>
</div>
</div>
<script type="text/javascript">
Vue.component('child',{
props:{
['temp']:Number,
},
template:'<span>{{temp*9/5+32}}</span>'
})
new Vue({
el:'#app',
data:{
ctemp:0
}
})
</script>
</body>
</html>
实验结果
实验3:颜色变化
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<style>
.active{
width:100px;
height:100px;
background:green;
}
.active2{
width:100px;
height:100px;
background:yellow;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="{active:isActive,active2:!isActive}">
<br>
</div>
<button v-on:click="isActive=!isActive">点击我</button>
</div>
<script>
new Vue({
el:'#app',
data:{
isActive:true
}
})
</script>
</body>
</html>
实验结果
点击前:
点击后:
实验4:数字输入
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
请输入1-10:<input type="text" v-model="type">
<div v-if="type>1&&type<10">
true
</div>
<div v-else>
false
</div>
</div>
<script>
new Vue({
el:'#app',
data:{
type:''
}
})
</script>
</body>
</html>