当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js生成随机颜色方法代码分享(三种)

js生成随机颜色方法代码分享(三种)

2019年04月01日  | 移动技术网IT编程  | 我要评论

话不多说,请看代码

<!doctype html>
<html>
<head lang="en">
 <meta charset="utf-8">
 <title></title>
</head>
<body>
  <button id="btn1">调用第一种</button>
  <button id="bnt2">调用第二种</button>
  <button id="btn3">调用第三种</button>
  <script>
   var btn1=document.getelementbyid('btn1');
   btn1.onclick=function(){
    document.body.style.background=bg1()
   };
   var btn2=document.getelementbyid('bnt2');
   btn2.onclick=function(){
    document.body.style.background=bg2();
   };
   var btn3=document.getelementbyid('btn3');
   btn3.onclick=function(){
    document.body.style.background=bg3();
   };
   function bg1(){
    return '#'+math.floor(math.random()*256).tostring(10);
   }
   function bg2(){
    return '#'+math.floor(math.random()*0xffffff).tostring(16);
   }
   function bg3(){
    var r=math.floor(math.random()*256);
    var g=math.floor(math.random()*256);
    var b=math.floor(math.random()*256);
    return "rgb("+r+','+g+','+b+")";//所有方法的拼接都可以用es6新特性`其他字符串{$变量名}`替换
   }
  </script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网