Vue组件的样式绑定和事件监听处理

本文介绍了Vue组件的样式绑定,包括通过v-bind为HTML标签绑定class属性和内联样式,强调了内联样式属性名的驼峰命名规则。接着详细讲解了Vue的事件监听和处理,如v-on(@)指令的使用,多事件处理,以及各种事件修饰符的应用,如捕获阶段监听、阻止事件传播、仅触发一次和防止默认行为等。
摘要由CSDN通过智能技术生成

一、Vue组件的样式绑定

1、为Html标签绑定class属性:v-bind,为class属性绑定一个对象,在对象中可以设置对象是否可用

1.1、绑定class属性,由绑定的变量来决定应用哪个样式

1.2、可以将样式直接设置成Vue组件中的数据对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<style>
    .blue{
     
        color: blue;
    }
    .red{
     
        color: red;
    }
    .green{
     
        color: green;
    }
    .font{
     
    font-size: 30px;
    }
</style>
<body>
    <div id="app">
        <div v-bind:class="{blue:isblue,red:isred}">
              西安
        </div>
        <div v-bind:class="[greenClass,fontClass]">
            郑州
        </div>
        <div :class="style">
            武汉
        </div>
    </div>
    <script>
        const obj={
     
            data(){
     
                return{
     
                    style:{
     
                     blue:true,
                     red:false
                    },
                        isblue:true,
                        isred:false,
                        greenClass:'green',
                        fontClass:'font',
                        textColor:'red',
                        textFont:'35px'
                }
            }
                }
        Vue.createApp(obj).mount('#app')
    </script>
</body>
</html>

注意:v-bind可以缩写为: <div :class=""></div>

在这里插入图片描述

2、绑定内联样式

<div :style=""></div>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<style>
    .blue{
     
        color: blue
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值