Blogger Information
Blog 70
fans 4
comment 5
visits 104900
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript:数学对象,随机生成数字验证码并且随机添加字体颜色
JiaJieChen
Original
947 people have browsed it

JavaScript:数学对象,随机生成数字验证码并且随机添加字体颜色

一.方法

方法 含义
Math.floor() 向下取整
Math.celi() 向上取整
Math.random() 随机生成0-1的数值

二.随机生成数字验证码并且随机添加字体颜色

三.代码块,可看注释

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>生成随机数</title>
  8. </head>
  9. <body>
  10. <div class="box">
  11. <span
  12. style="background-color: lightblue; font-weight: 600; padding: 5px"
  13. ></span>
  14. </div>
  15. <script>
  16. //生成0-1的随机数 random
  17. // console.log(Math.floor(Math.random() * 4));
  18. //随机生成验证码
  19. //首先建立一个字符串
  20. let str =
  21. "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNNM1234567890";
  22. //把字符串转为数组,并且分隔开字符
  23. let arr = str.split("");
  24. // 建立一个储存数值的变量
  25. let result = "";
  26. for (let i = 0; i < 4; i++) {
  27. let n = Math.floor(Math.random() * arr.length);
  28. result += arr[n];
  29. }
  30. // 随机生成颜色
  31. function show() {
  32. let r = Math.floor(Math.random() * (255 + 1));
  33. let g = Math.floor(Math.random() * (255 + 1));
  34. let b = Math.floor(Math.random() * (255 + 1));
  35. let rgb = "rgb" + "(" + r + "," + g + "," + b + ")";
  36. return rgb;
  37. }
  38. // console.log(show());
  39. //把随机生成的字符添加到span里面
  40. let yzm = document.querySelector(".box > span").append(result);
  41. ////给span添加随机颜色
  42. let Span = (document.querySelector(".box >span").style.color = show());
  43. </script>
  44. </body>
  45. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post