zoukankan      html  css  js  c++  java
  • vue中的样式绑定

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>vue中的样式绑定</title>
        <script src="./vue.js"></script>
        <style>
            .activated {
                color: red;
            }
            .activated-one{
                font-size: 78px;
            }
        </style>
    </head>
    <body>
    <div id="app">
        <div @click='handleDivClick'
             :class="[activated,activatedOne]">
            <!--:class = "{activated:isActivated}"-->
            <!--class对象绑定-->
            hello world
        </div>
    </div>
    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                // isActivated:false
                activated: '',
                activatedOne:''
            },
            methods: {
                handleDivClick: function () {
                    // this.isActivated = !this.isActivated  //取反
                    // if (this.activated === "activated") {
                    //     this.activated = '';
                    // } else {
                    //
                    //     this.activated = "activated";     //代码过于冗余
                    // }
                    this.activated = this.activated === "activated" ? "" : "activated"  //三元表达式
                    this.activatedOne = this.activatedOne === "activated-one" ? "" : "activated-one"  //三元表达式
                }
            }
    
        })
    </script>
    </body>
    </html>
    
    <!--
    两种方法:一个对对象进行绑定,一个是对数组进行绑定
    -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Vue中内联样式绑定</title>
        <script src="./vue.js"></script>
    </head>
    <body>
        <div id="app">
            <!--<div :style="styleObj" @click="handleDivClick">-->
            <div :style="[styleObj,{fontSize:'20px'}]" @click="handleDivClick">
                hello world
            </div>
        </div>
        <script>
            var vm = new Vue({
                el:"#app",
                data:{
                  styleObj:{
                      color:"black"
                  }
                },
                methods:{
                    handleDivClick:function () {
                        this.styleObj.color = this.styleObj.color === 'black' ? "red" : "black";
                    }
                }
            })
        </script>
    </body>
    </html>
    
    <!--
    两种方法:一个对对象进行绑定,一个是对数组进行绑定
    -->
  • 相关阅读:
    古老当时兴
    购买代购的产品算违法吗——看空姐代购被判刑有感
    七种方法让你的网站在搜索结果中突围而出(中)
    amf webgame
    游戏开发协议(转)
    array的排序
    用 javascript + actionScript 解决透明的flash在firefox下滚轮失效的问题!(转)
    知乎摘 励志回答
    ie:stagewidth,stageheight的bug
    jsfl bug解决
  • 原文地址:https://www.cnblogs.com/xuyxbiubiu/p/9961960.html
Copyright © 2011-2022 走看看