Vue数据与事件绑定以及Class和Style的绑定详细讲解

4. 事件绑定中的修饰符

​ (1).prevent:调用preventDefault()。阻止链接打开URL

<a href="http://www.bilibili.com" rel="external nofollow"  v-on:click.prevent>bilibili</a>

(2).stop:调用stopPropagation()。阻止事件传播(阻止事件冒泡)

​(3)键值修饰符:在监听键盘事件时,需要知道键的keyCode,记忆不方便,可以通过修饰符来记录键值

​ enter、tab、delete、esc、space、up、down、left、right

    <script src="../js/vue.js"></script>
<body>
    <div >
        <button v-on:keyup.up="say">提交</button>
    </div>
    <script>
        const vm = new Vue({
            el:'#app',
            data:{
                msg:'键盘修饰符',
            },
            methods:{
                say(){
                    alert(this.msg)
                }
            }
        })
    </script>
</body>

三、Class和Style的绑定

1. 对象语法:给v-bind:class传递一个对象,来动态地改变class属性值

    <script src="../js/vue.js"></script>
<style>
    div {
        width: 100px;
        height: 100px;
    }
    .class1 {
        background-color: #ff0;
    }
    .class2 {
        background-color: #f00;
    }
</style>
<body>
    <div  v-bind:class="colorName" v-on:click="changeColor"></div>
    <script>
        const vm = new Vue({
            el: '#app',
            data:{
                colorName:{
                    class1:true,
                    class2:false
                }
            },
            methods: {
                changeColor() {
                    this.colorName.class1 = !this.colorName.class1
                    this.colorName.class2 = !this.colorName.class2
                }
            }
        })
    </script>
</body>

2. 数组语法:可以把一个数组传给v-bind:class,以应用一个class列表

    <script src="../js/vue.js"></script>
<style>
    div {
        width: 100px;
        height: 100px;
    }
    .class1 {
        background-color: #ff0;
    }
    .class2 {
        background-color: #f00;
    }
</style>
<body>
    <div  v-bind:class="[c1,c2]" v-on:click="changeColor"></div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                c1: 'class1',
                c2: '',
            },
            methods: {
                changeColor() {
                    this.c1 = (this.c1 == '' ? 'class1' : '')
                    this.c2 = (this.c2 == '' ? 'class2' : '')
                }
            }
        })
    </script>
</body>

3. 内联样式绑定:直接绑定style(v-bind:style)

(1)对象表示法

    <script src="../js/vue.js"></script>
<style>
    div {
        width: 100px;
        height: 100px;
    }
    .class1 {
        background-color: #ff0;
    }
    .class2 {
        background-color: #f00;
    }
</style>
<body>
    <div  v-bind:class="[c1,c2]" v-on:click="changeColor">
        <div v-bind:>白桦林</div>
    </div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                c1: 'class1',
                c2: '',
                fontColor:'blue',
                mySize:'18px'
            },
            methods: {
                changeColor() {
                    this.c1 = (this.c1 == '' ? 'class1' : '')
                    this.c2 = (this.c2 == '' ? 'class2' : '')
                }
            }
        })
    </script>
</body>

(2)数组表示法

    <script src="../js/vue.js"></script>
<style>
    div {
        width: 100px;
        height: 100px;
    }
    .class1 {
        background-color: #ff0;
    }
    .class2 {
        background-color: #f00;
    }
</style>
<body>
    <div  v-bind:class="[c1,c2]" v-on:click="changeColor">
        <div v-bind:>白桦林</div>
    </div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                c1: 'class1',
                c2: '',
                colorStyle:{color:'red'},
                fontStyle:{fontSize:'32px'}
            },
            methods: {
                changeColor() {
                    this.c1 = (this.c1 == '' ? 'class1' : '')
                    this.c2 = (this.c2 == '' ? 'class2' : '')
                }
            }
        })
    </script>
</body>

原文地址:https://blog.csdn.net/m0_74343097/article/details/128563671