麦克风的纹路 2021-05-21 07:09 采纳率: 85.7%
浏览 133
已采纳

Vue做一个点击变色的按钮?

点击了,按钮自己颜色会变化的,如果能变色为随机色就更好?有这样的实例吗??

  • 写回答

5条回答 默认 最新

  • Proud lion 2021-05-21 10:01
    关注
    <!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>
    </head>
    
    <body>
        <style>
            #app{
                text-align: center;
            }
            button {
                width: 200px;
                height: 80px;
                border: 0px;
                border-radius: 10px;
                font-size: 24px;
                color: rgb(255, 255, 255);
                margin-top: 180px;
                cursor: pointer;
            }
        </style>
        <div id="app">
            <button :style="{background:color}" @click="changeColor">点击变色</button>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            var app = new Vue({
                el: "#app",
                data: {
                    color: 'rgb(0,251,63)'
                },
                methods: {
                    changeColor() {
                        this.color =`rgb(${parseInt(Math.random()*255)},${parseInt(Math.random()*255)},${parseInt(Math.random()*255)})`
                    }
                },
            })
        </script>
    </body>
    
    </html>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?