首先看代码

<!DOCTYPE html>
<html lang=en>

<head>
    <meta charset="utf-8" />
    <title>Hello world</title>
    <script src="vue.js"></script>
    <style>
        .activated {
            color: red;
        }

        .activated-one {
            font-size: 50px;
        }
    </style>
</head>

<body>
    <div id="root">

        <!--class数组绑定 后面加一个数组,可以写多个值,数组的值会被vue中的值代替 -->
        <div @click="handleDivClick" :class="[activated,activatedOne]">HelloWorld</div>
    </div>


    <script>
        var vm = new Vue({
            el: "#root",
            data: {
                activated: "",
                activatedOne: "activated-one"
            },
            methods: {
                handleDivClick: function () {
                    if (this.activated === "activated") {
                        this.activated = "";
                    } else {
                        this.activated = "activated";
                    }
                }
            }
        })

    </script>
</body>

</html>

vue绑定class属性,后面跟着要给数组,数组的值就是vue实例中变量的值,把样式的名称赋值给变量,渲染后就赋给了class属性。

Logo

前往低代码交流专区

更多推荐