$event理解

2022-11-05  本文已影响0人  璃安_
<!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>Document</title>
    <!-- 引入库文件 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>count的值为{{count}}</p>
        <input type="button" value="按钮" @click="add($event,1)">
    </div>

    <script>
        // 创建实例对象
        const app = new Vue({
            el: '#app',
            data: {
                count: 1
            },
            methods: {
                add (e, a) {
                    console.log(e, a);
                    this.count += a
                    console.log(this.count);


                    if (this.count % 2 === 0) {
                        e.target.style.backgroundColor = 'pink'
                    } else {
                        e.target.style.backgroundColor = 'skyblue'

                    }
                }
            }
        })
    </script>
</body>

</html>
image.png
image.png
image.png
上一篇下一篇

猜你喜欢

热点阅读