vue v-bind:style 方法传参改变button颜色!

本文详细介绍了如何在Vue.js中利用v-bind:style指令动态地改变button元素的颜色。通过将颜色值作为参数绑定到样式对象,可以实现按钮颜色的动态响应,为用户体验增加更多交互性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<style>
		
	</style>
	<body>
		<template id='tmp'>
				<button v-bind:style='getStyle(type)'>{{title}}</button>
			</template>
			<div id="app">
			    <!--使用定义好的全局组件-->
			    <tom-button title="hello" type='success'></tom-button>
				<tom-button title="Angularjs" type='danger'></tom-button>
				<tom-button title="Angularjs" type='normal'></tom-button>
			</div>
			</body>
			
			<script type="text/javascript">
			    // 定义全局组件,两个参数:1,组件名称。2,组件参数
			    Vue.component("tom-button",{
			        template:'#tmp',
					props:['title', 'type'],
					methods: {
						getStyle(type) {
							console.log(type);
							if (type=='success')
							   return {border: 'green 2px solid'}
							if (type=='danger')
							   return {border: 'red 2px solid'}
							return {border: 'gray 2px solid'}
						}
					}
			    })
			    var app = new Vue({
			        el:"#app"
			    })
			</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值