v-model & v-bind

v-model 双向绑定

修饰符 :

  1. .lazy 当在输入框输入内容的时候不立即响应,失焦的时候数据才双向绑定
  2. .number 输入框内只能输入数字,当输入其它
  3. .trim
  4. 文本框应用
  5. 单选应用
  6. 复选应用
  7. 下拉列表应用
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="app">
			<h4>文本框</h4>
			<input class="box1" v-model.lazy="value">
			<input class="box1" v-model.number="value">
			{
    
    {
    
     value }}
			
			<h4>单选</h4>
			<input type="radio"  v-model="sex" id="" value="男" /><input type="radio"  v-model="sex" id="" value="女" /><h4>复选</h4>
			城市:
			<input type="checkbox" v-model="city" value="长治" />长治
			<input type="checkbox" v-model="city" value="运城" />运城
			<input type="checkbox" v-model="city" value="太原" />太原
			
			<h4>下拉列表</h4>
			<select name="" v-model="items">
				<option value="java">java</option>
				<option value="html">html</option>
				<option value="php">php</option>
			</select>
			{
    
    {
    
     items }}
		</div>
		<script type="text/javascript">
			new Vue({
    
    
				el : '#app',
				data : {
    
    
					value : 'd',
					sex : '女',
					city : ['长治','太原'],
					items : 'html'
				},
				
			})
		</script>
	</body>
</html>

v-bind 绑定属性

1.绑定style属性
案例:点击按钮切换文本颜色

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<style type="text/css">
			.green{
    
    
				color: green;
			}
			.red{
    
    
				color: #FF0000;
			}
			.blue{
    
    
				color : blue;
			}
		</style>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="app">
			<button type="button" @click="change('red')">红色</button>
			<button type="button" @click="change('green')">绿色</button>
			<button type="button" @click="change('blue')">蓝色</button>
			
			<p :style="{color : colorType}">{
    
    {
    
     msg }}</p>
		</div>

		<script type="text/javascript">
			
			new Vue({
    
    
				el: '#app',
				data: {
    
    
					msg : 'yyqx',
					item : 'item',
					red : false,
					green : false,
					blue : false,
					colorType : '红色'
				},
				methods:{
    
    
					change(name){
    
    
						this.colorType = name;
					}
				}
			})
			
		</script>
	</body>
</html>

在这里插入图片描述
2. 绑定class属性
案例:点击按钮切换文本颜色

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<style type="text/css">
			.green{
    
    
				color: green;
			}
			.red{
    
    
				color: #FF0000;
			}
			.blue{
    
    
				color : blue;
			}
		</style>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="app">
			<button type="button" @click="red = true">红色</button>
			<button type="button" @click="green = true">绿色</button>
			<button type="button" @click="blue = true">蓝色</button>
			<p :class="{red,green,blue}">{
    
    {
    
     msg }}</p>
		</div>

		<script type="text/javascript">
			
			new Vue({
    
    
				el: '#app',
				data: {
    
    
					msg : 'yyqx',
					item : 'item',
					red : false,
					green : false,
					blue : false,
				},
				methods:{
    
    
					change(name){
    
    
						if(name == 'red'){
    
    
							this.red = true;
							this.green = false;
							this.blue = false;
						}else if(name == 'green'){
    
    
							this.green = true;
							this.blue = false;
							this.red = false;
						}else if(name == 'blue'){
    
    
							this.blue = true;
							this.red = false;
							this.green = false;
						}
					}
				}
			})
			
		</script>
			
	</body>
</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45021462/article/details/109487265
今日推荐