JS-改变页面的颜色之变化核心-获取六位的随机数

前言:JS-改变页面的颜色(一)JS-改变页面的颜色(二)JS-改变页面的颜色(三)三个简单的小白例,我们可以轻而易举的看到起变化的核心是——十六进制颜色值的获取方式,所以,我们这里总结一下获取六位随机数的方法都有那些。

代码比较简单就不一个个解释了,不过总体的思路这里要简单的记录一下:

一:需求,获取六位的数字随机数

二:思路,关键就是怎么获取变化的数字

1)通过前端的随机函数,来获取随机数,可以获取一位或者多位然后通过循环来拼接成六位,或者我们想要的任何位数

2)获取随机数,除了通过随机函数,就是通过获取当前时间的毫秒后六位了,不过这样前面三位雷同的比较多,可以选择使用随机函数和毫秒数组合的方式来组合

3)除了前端获取也可以通过发送请求到后台来获取,这样不同的后台语言有不同的方式,不过最总还是少不了使用随机函数的,只是使用的方式会有所变化

4)参考资料Math对象的相关方法

5)这里提供了获取六位随机数的思路,不过我们可以举一反三,获取任何的随机数,也可以通过一定范围内的随机和数组结合获取我们想要的任何随机字符的组合,这也是前端简单的验证码实现的一种思路

1.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = ""+Math.round(Math.random()*1000000);
           while(randomNum.length<6){randomNum="0"+randomNum;}
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

2.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = ""+Math.floor(Math.random()*1000000);;
           while(randomNum.length<6){randomNum="0"+randomNum;}
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

3.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = ""+Math.ceil(Math.random()*1000000);;
           while(randomNum.length<6){randomNum="0"+randomNum;}
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

4.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = (Math.random()+"").substr(2,6);
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

5.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = (Math.random()+"").substring(2,8);
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

6.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = "";
           for(var i=0;i<6;i++)
           {
            randomNum+=Math.floor(Math.random()*10);
           }
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

7.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = (function(t){ var str =‘‘; while (t--){ str += ~~(Math.random()*10) }; return str; })(6);
           console.info("randomNum is ========",randomNum,~~(Math.random()*10));
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

8.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = new Date().getTime()+‘‘;
           randomNum = (Math.floor(Math.random() * 9) + 1).toString()+randomNum.substring(randomNum.length-5,randomNum.length);
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

9.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNumTemp = /\d{5}$/.exec(+new Date()+‘‘);
           var randomNum = (Math.floor(Math.random() * 9) + 1).toString()+randomNumTemp[0];
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

10.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = Math.random()*900000|0+100000;
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

11.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = Math.floor(Math.random()*900000 + 100000);
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

12.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = (‘‘+Math.random()).match(/\d{6}/)[0]
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>

13.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Get Random Num</title>
    <script>
        function getRandomNum()
        {
           var randomNum = (‘‘+Math.random()).match(/[^0\.]\d{5}/)[0]
           console.info("randomNum is ========",randomNum);
           return randomNum;
        }
    </script>
</head>
<body bgcolor="LightGoldenRodYellow" align="center">
    <input type="button" value="Please click ME and F12 !" onclick="getRandomNum();"/>
</body>
</html>
时间: 2024-08-04 00:57:51

JS-改变页面的颜色之变化核心-获取六位的随机数的相关文章

JS-改变页面的颜色(二)

需求:点击页面的按钮,改变页面的颜色 思路:一先画出最简单的页面,二想办法获取页面的body节点,三想办法修改body节点的背景颜色属性,四通过一个方法获取随机的颜色值           和第一个例子(JS-改变页面的颜色(一))相比仅仅是改变了生成颜色值的思路 简单的代码片段如下所示: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Change P

JS-改变页面的颜色(三)

需求:点击页面的按钮,改变页面的颜色 思路:一先画出最简单的页面,二想办法获取页面的body节点,三想办法修改body节点的背景颜色属性,四通过一个方法获取随机的颜色值           和第二个例子(JS-改变页面的颜色(二))相比仅仅是改变了生成颜色值的思路 简单的代码片段如下所示: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Change P

JS实现网页背景颜色与select框中的颜色同时变化

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

用JS让下拉框改变网页背景颜色

<HTML> <HEAD> <TITLE>石家庄渣浆泵配件</TITLE> </HEAD> <SCRIPT> <!-- function bgChange(selObj) { newColor = selObj.options[selObj.selectedIndex].text; document.bgColor = newColor; selObj.selectedIndex = -1; } //--> </SC

as3.0和js相互调用,js控制flash的颜色

程序应用场景:flash是一副地图,地图上一单击,就调用js,弹出一个对话框,显示一些自己想要的信息:js定时调用as提供的方法,然后去改变flash的颜色. 以下是as代码: import flash.events.MouseEvent; import flash.text.TextField; //给每一栋楼增加监听事件 mc0001.addEventListener(MouseEvent.CLICK,onClick); mc0004.addEventListener(MouseEvent.

config.js配置页面中的样式和图片路径

这个文章用在什么地方,我先说一下,上周啊,我接到一个任务.因为公司业务要对接不同的银行,例如在工行下颜色是红色的,在其他银行下默认为蓝色,所以在页面一致的情况下,保证页面中的按钮和ICON是可以配置的,这样秩序改动一个值[颜色或路径],就能正常全部适配好了,其实这个业务很简单: **第一种方案:我们有新建两个config1.js和config2.js,代码分别类似如下:** var config = { // 改变全局按钮颜色 btncolor: "red", // 配置优惠券和同意的

CSS改变插入光标颜色caret-color简介及其它变色方法(转)

一.CSS改变输入框光标颜色的原生属性caret-color CSS caret-color属性可以改变输入框插入光标的颜色,同时又不改变输入框里面的内容的颜色. 例如: input { color: #333; caret-color: red; } 结果光标颜色变成红色,文字还是深黑色: 眼见为实,您可以狠狠的点击这里:CSS caret-color改变光标颜色demo //zxx: 单词caret表示"插入符号",指处于内容可插入状态的光标. caret-color属性不仅对于原

Node.js文件模块fs监视文件变化

Node.js文件模块fs监视文件变化 Node中文件模块fs监视文件的函数源码如下: fs.watch = function(filename) { nullCheck(filename); var watcher; var options; var listener; if (util.isObject(arguments[1])) { options = arguments[1]; listener = arguments[2]; } else { options = {}; listen

php开发之js修改页面css样式

在我以前的印象里,页面的字体属性,背景,等样式在页面加载后基本上都是固定的了,但是今天看到可以通过js修改页面的样式,觉得有必要和大家分享下. test.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equi