1.计数器

计数器

思路

设计两个按钮,分别添加点击事件,实现累加和递减。

知识点

  1. 事件绑定
v-on:click && @click 绑定事件

2.获取元素内容

  • v-text:获取标签里的文本,不渲染html标签
        <h1 v-text="text">
        </h1>
  • v-html:获取标签里的内容,会渲染html标签
        <h1 v-html="text">
        </h1>
  • 直接通过模板获取
<h1>{{text}}</h1>

此处实现计算器的数字通过模板获取

代码

<!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">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            list-style: none;
            text-decoration: none;
        }

        #app {
            width: 150px;
            height: 50px;
            background-color: #00ffff38;
            margin: auto;
            margin-top: 60px;
            display: flex;
        }

        button {
            width: 50px;
            height: 50px;
            border: none;
            background-color: #5f9ea09c;
            cursor: pointer;
            font-size: 20px;
        }

        span {
            display: block;
            width: 50px;
            height: 50px;
            background-color: aquamarine;
            text-align: center;
            line-height: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="jian">
            -
        </button>
        <span>
            {{num}}
        </span>
        <button @click="add">
            +
        </button>
    </div>
</body>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            num: 1
        },
        methods: {
            add: function () {
                if (this.num < 10) {
                    this.num++;
                } else {
                    alert("到顶了!");
                }
            },
            jian: function () {
                if (this.num > 0) {
                    this.num--;
                } else {
                    alert("到低了!");
                }
            }
        }
    })
</script>

</html>
posted @ 2021-10-26 13:01  禾耳  阅读(58)  评论(0)    收藏  举报