weixin_48163062 2021-10-08 18:57 采纳率: 0%
浏览 37

js怎么随机颜色一个方块点击时在方框里显示16进制颜色值

两个div方块第一个是用来显示16进制颜色值的,第二个就是随机颜色的方块,
点击第二个方块的时候,第一个div里面会显示这个16进制的颜色值

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-10-08 20:55
    关注

    题主要的代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

    img

    
    <!doctype html>
    <style>
        #div1,#div2{float:left;width:80px;height:80px;border:solid 1px #999;margin-right:20px;font-size:12px;text-align:center;line-height:80px}
    </style>
    <div id="div1"></div>
    <div id="div2"></div>
    
    <input type="button" value="切换颜色" onclick="rndColor()"/>
    <script>
        var div1 = document.getElementById('div1'), div2 = document.getElementById('div2'), color;
        function addZero(v) { if (v.length < 2) return '0' + v; return v;}
        function rndColor() {
            div1.innerHTML = '';
    
            div2.style.backgroundColor = color = '#'
                + addZero(Math.floor(Math.random() * 256).toString(16))
                + addZero(Math.floor(Math.random() * 256).toString(16))
                + addZero(Math.floor(Math.random() * 256).toString(16));
        }
        div2.onclick = function () { div1.innerHTML = color }
        rndColor()
    </script>
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月8日

悬赏问题

  • ¥15 mySQL5.7.34安装遇到的问题
  • ¥15 结构功能耦合指标计算
  • ¥20 visual studio中c语言用ODBC链接SQL SERVER
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊