属性指令

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
<div id="app">
    <h2>v-bind:属性名="变量"</h2>
    <!--    -标签会有很多属性 <a href=""></a>   <img src="" >  <p name="xxx"></p> <button class="btn">点我</button>-->
    <!--    -可以绑定标签的任意属性-->
    <!--    -v-bind可以简写成  :属性='变量'-->
    <button @click="clickEvent">click</button>
    <img v-bind:src="url" alt="img">
    <img :src="url" alt="img">
</div>
</body>
<script>
    <!--    创建app实例-->
    const app = Vue.createApp({
            data() {
                return {
                    url: 'https://www4.bing.com//th?id=OHR.BridalVeilFalls_ZH-CN3954641670_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp&w=240&h=135',
                    imgScr: ['https://www4.bing.com//th?id=OHR.BridalVeilFalls_ZH-CN3954641670_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp&w=240&h=135', 'https://www4.bing.com//th?id=OHR.GlassOctopus_ZH-CN6853414529_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp&w=240&h=135'],
                }
            }, methods: {
                clickEvent() {
                    this.url = this.imgScr[Math.round(Math.random())]
                },
            }
        }
    )
    //挂载
    app.mount('#app')

</script>
</html>

class/style

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>
    <style>
        .red {
            color: red;
        }

        .bg {
            background-color: yellow;
        }

        .fz {
            font-size: 50px;
        }
    </style>
</head>
<body>
<div id="app">
    <h1>class的三种绑定方式</h1>
    <span v-text="str1" v-bind:class="class_obj"></span>
    <br>
    <span v-text="str1" :class="class_str"></span>
    <br>
    <span v-text="str1" :class="class_str"></span>
    <h1>style的三种绑定方式</h1>
    <span v-text="str1" v-bind:style="style_str"></span>
    <br>
    <span v-text="str1" :style="style_list"></span>
    <br>
    <span v-text="str1" :style="style_list2"></span>
    <br>
    <span v-text="str1" :style="style_obj"></span>
</div>

</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                str1: 'YxbHgdMu6C'.toUpperCase(),
                // class:字符串,数组(常用,class由多个类组成,数组跟它最搭),对象
                class_str: 'red fz bg',
                class_list: ['red', 'fz'],
                class_obj: {
                    red: true,
                    bg: true,
                    fz: false
                },
                // style :字符串,数组,对象(style 来讲,它用的多)
                style_str: 'color:blue;background: pink',
                style_list:[{'font-size':'100px'}],
                //驼峰形式
                style_list2:[{fontSize:'200px'}],
                style_obj:{
                    fontSize:'50px'
                }
            }
        },
        methods: {}
    })
    app.mount('#app')
</script>
</html>
posted @ 2022-10-18 20:29  Sherwin_szw  阅读(12)  评论(0编辑  收藏  举报