随机一个十六进制颜色

阳光穿透心脏的1/2处 2023-06-05 08:12 121阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. width: 300px;
  11. height: 200px;
  12. box-shadow: 0 0 5px lightblue;
  13. font-size: 24px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div>this is div</div>
  19. <script>
  20. var div = document.querySelector('div');
  21. // console.log(div);
  22. // 封装函数
  23. function getColor() {
  24. //#号
  25. var str = '#';
  26. // 0-e的数组
  27. var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  28. // 循环6次
  29. for (var i = 0; i < 6; i++) {
  30. // 随机这16位中的数字字母
  31. var num = parseInt(Math.random() * 16);
  32. // 把每一项添加给str
  33. arr[num];
  34. str += arr[num];
  35. }
  36. return str;
  37. }
  38. // console.log(getColor());
  39. // 文档注册点击事件, 点击随机div颜色
  40. document.onclick = function () {
  41. div.style.backgroundColor = getColor();
  42. div.style.color = getColor();
  43. }
  44. </script>
  45. </body>
  46. </html>

文章地址 https://www.cnblogs.com/sandraryan/

转载于:https://www.cnblogs.com/sandraryan/p/11610085.html

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读

    相关 随机颜色

    随机颜色 开发工具与关键技术:JavaScript 作者:陈希雄 撰写时间:2019/2/19 提醒:其实也可以拼接成自己想要的图形或者文字 操