<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>Document</title>
    <style type="text/css">
        .active{
            color: red;
        }
    </style>
</head>
<body>
    <div id="app">
    <h1 v-bind:class="{active:isActive,line:isLine}">{{message}}</h1>
    <button v-on:click="change">改变颜色</button>
    </div>
</body>
<script type="text/javascript">
    // v-on:click   绑定一个点击事件
    // vi-on:click = ‘xx’ 绑定方式直接在后面增加一个方法名称即可,不用xx()
    // 设计原理是给h1初始化的加上两个class 属性,分别是 active 和 line,通过取反的方式删除掉一个isActive值,变成 false
    //class = {} 这个是属性的对象写法
    //对象写法中,通常以 {键:值,键:值},对象写法在vue和日常使用中比较常用
    //methods,是Vue的方法挂载方式
    //在methods中,方法的写法为 function:function(){},而不是传统的JS写法
    //
    let app = new Vue({
        el:’#app’,
        data:{
            message:’echo a red word’,
            isActive:true,
            isLine:true
        },
        methods:{
            change:function(){
                this.isActive = !this.isActive;
            }
        }
    })
</script>
</html>
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号