Vue入门教程--对元素进行样式绑定详解(article:6)

Vue.js 初步入门


方法简介

  1. 使用class绑定样式。
  2. 使用内联样式style绑定。

使用详情

原生js中,样式的绑定有两种,第一种是直接在标签属性style里赋予自己想自定义的属性:

<div id="container">
    <div style = "color:'blue'">
        {{hello}}
    </div>
</div>
let app = new Vue({
		el:"#container",
		data:{
			hello:"hello vue.js"
		},
		methods:{
			
		}
});

第二种是给标签定义id或者class,然后在样式域给特定的id或class不同的样式:

	<style type="text/css">
		.blue{
			color:blue;
			background-color: #FFC0CB;
		}
		.thin{
			font-weight: 150;
		}
		.size{
			width: 100px;
			height: 100px;
		}
	</style>
<div id="container">
    <div class="blue size">
        {{hello}}
    </div>
</div>
let app = new Vue({
		el:"#container",
		data:{
			hello:"hello vue.js"
		},
		methods:{
			
		}
});

- 使用class绑定样式

vue给用户提供了属性绑定,让用户能够灵活的对class属性进行赋值。

	<style type="text/css">
		.blue{
			color:blue;
			background-color: #FFC0CB;
		}
		.thin{
			font-weight: 150;
		}
		.size{
			width: 100px;
			height: 100px;
		}
	</style>
1.数组
<div :class="['blue','size']">
	{{hello}}
</div>
2. 数组中使用三元表达式
<div :class="['blue','size',isactive?'thin':'']">
	{{hello}}
</div>
data:{
	hello:"hello vue.js",
	isactive:false
}
3.数组中嵌套对象
<div :class="['blue','size',{thin:isactive}]">
	{{hello}}
</div>
4. 直接使用对象
<div :class="{blue:true,thin:false,size:isactive}">
	{{hello}}
</div>

在这里插入图片描述

- 使用class绑定样式
1. 直接在元素上通过:style传入对象进行样式的绑定
<div :style="{color:blue,'font-weight': 150}">
	{{hello}}
</div>
2. 将样式写到数据域中,再引入
data:{
	hello:"hello vue.js",
	isactive:true,
	myStyle1:{color:'blue','font-weight':'150'}
}
<div :style="myStyle1">
	{{hello}}
</div>
3. 将多个样式数据放到数据域中,并用数组引用多个样式对象
data:{
	hello:"hello vue.js",
	isactive:true,
	myStyle1:{color:'blue','font-weight':'150'},
	myStyle2:{'font-size':'40pt',width:'100px',height:'100px'}
}
data:{
	hello:"hello vue.js",
	isactive:true,
	myStyle1:{color:'blue','font-weight':'150'},
	myStyle2:{'font-size':'40pt',width:'100px',height:'100px'}
}

在这里插入图片描述


Vue中动态绑定v-model,有几种方法可以实现。其中一种方法是使用一个数组来存储动态生成的input的值,并使用v-model绑定到数组的每个元素上。 首先,创建一个空的数组,比如selectVal: []。然后,使用v-model绑定到每个input上,如v-model="selectVal[index].value"。接下来,使用循环其他方式动态生成需要绑定v-model的input的数量,比如使用for循环生成10个input。 示例代码如下: ``` data() { return { selectVal: [] // 创建一个空的数组 } }, mounted() { var len = 10; for (var i = 0; i < len; i++) { var item = { value: '' }; // 创建一个对象,用来存储input的值 this.selectVal.push(item); // 将对象添加到数组中 } } ``` 在上述代码中,通过循环生成了10个需要绑定v-model的input,将每个input的值存储在selectVal数组中的对应元素的value属性中。 通过这种方式,可以实现动态绑定v-model,并且根据接口返回的数据动态生成相应数量的input。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [在 vue.js 中动态绑定 v-model](https://blog.youkuaiyun.com/qq_30944053/article/details/88634779)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [vue v-model动态生成详解](https://download.youkuaiyun.com/download/weixin_38696336/14904156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值