<!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>改变颜色</title>

    <script src=”/vue.js”></script>

    <style>

        .divClass{

            border: 1px solid red;

            width: 200px;

            height: 200px;

        }

        .divClass2{

            background-color:  rgb(0, 0, 0); 

        }

        .divClass3{

            background-color: goldenrod; 

        }

    </style>

</head>

<body>

  

    <div id=”app”>

        <div :class=”{divClass:isDivClass, divClass2:isDivClassX, divClass2:isDivClassY}”>

                <h1 :style=”color”>看颜色</h1>

        </div>

        <button @click=”change”>点击切换 背景</button>

    </div>

    <script>

        new Vue({

            el:’#app’,

            data(){  // data以后尽量都这么写

                return{

                    isDivClass:true,

                    isDivClassX:true, // 对象的绑定

                    isDivClassY:false,  

                    isChange:true,

                    color:’color:yellow’ 

                }

            },

            methods: {

                change(){ 

                    if( this.isChange ){ 

                         this.isDivClassY=!this.isDivClassY;

                        this.color = ‘color:goldenrod’; 

                        this.isChange==false

                    }else{  

                        this.isDivClassX=!this.isDivClassX;

                        this.color = ‘color:yellowgreen’; 

                        this.isChange==true

                    }

                    }

                }

        })

    </script>

</body>

</html>


版权声明:本文为OpenSource_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/OpenSource_/article/details/120950318