class与style

  1. Binding HTML Class
    • - object syntax
    • - array syntax
  2. Binding inline style
    • - object syntax
    • - array syntax
    • // need to font-size => fontSize
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Examples</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <script src="http://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        .red {
            background-color: red;
        }
        .yellow {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div id="box">

        <button @click="handleClick()">click</button>
        <div :class="isActive?'red':'yellow'">我是动态绑定class-三目写法</div>
        <div :class="classobj">我是动态绑定class-对象写法</div>
        <div :class="classarr">我是动态绑定class-数组写法</div>

        <div :style="'background:'+(isActive?'red':'yellow')">我是动态绑定style-三目写法</div>
        <div :style="styleobj">我是动态绑定style-对象写法</div>
        <div :style="stylearr">我是动态绑定style-数组写法</div>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#box",
            data: {
                isActive: true,
                classobj: {   //class-对象写法
                    a: true,
                    b: true
                    // a b, class名字
                },
                classarr: ["a", "b"],  //class-数组写法
                styleobj: {   //style-对象写法
                    backgroundColor: "red"
                },
                stylearr: []    //style-数组写法
            },
            methods: {
                handleClick() {
                    this.isActive = !this.isActive  //取反操作
                }
            }
        })
    </script>
</body>
</html>
Released five original articles · won praise 0 · Views 27

Guess you like

Origin blog.csdn.net/qq_46606159/article/details/105009408
Recommended