生成随机颜色代码为
this.color = rgb(${Math.round(Math.random()*255)},${Math.round(Math.random()*255)},${Math.round(Math.random()*255)});

<!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>
    <div class="app">
        <p :style="{'color': color}" @click = "ch">{{msg}}
        </p>
        <p>点击{{number}}次</p>
        <!-- 给p设置一个style的绑定color 点击得到随机颜色 并且给后面加上点击的次数 -->
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <script>
        var vm = new Vue({
            el : ".app",
            data : {
                msg : "点击我",
                number : 0,
                color : "black"
            },
            methods: {
                ch (){
                    this.color = `rgb(${Math.round(Math.random()*255)},${Math.round(Math.random()*255)},${Math.round(Math.random()*255)})`;
                    this.number++;
                }
            }
        })
    </script>
</body>
</html>
Logo

前往低代码交流专区

更多推荐