基于JavaScript实现随机颜色输入框
网络编程 2021-07-04 19:19www.168986.cn编程入门
这篇文章主要介绍了基于JavaScript实现随机颜色输入框的实例代码,代码简单易懂,非常不错,需要的朋友参考下吧
废话不多说了额,具体代码如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> table{width:500px;height:400px;} table td{width:100px;height:50px;} </style> <script> window.onload=function(){ var inps=document.getElementsByTagName('input'); var btn=document.getElementsByTagName("button")[0]; //点击获取table btn.onclick=function(){ var table=document.createElement("table"); var tbody=document.createElement("tbody"); var n=inps[0].value; var m=inps[1].value; for(var i=1;i<=n;i++){ var tr=document.createElement("tr"); //循环时获取颜色值 for(var j=1;j<=m;j++){ var td=document.createElement("td"); tr.appendChild(td); var color="rgb("+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+"," +parseInt(Math.random()*256)+")"; td.style.backgroundColor=color; } inps[0].value=""; inps[1].value=""; tbody.appendChild(tr); table.appendChild(tbody); document.body.appendChild(table); } } } </script> </head> <body> 行:<input type="text" value=""/> 列:<input type="text" value="" /> <button>获取随机颜色</button> </body> </html>
以上所述是长沙网络推广给大家介绍的基于JavaScript实现随机颜色输入框,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,长沙网络推广会及时回复大家的!
上一篇:JS产生随机数的用法小结
下一篇:jQuery Ajax请求后台数据并在前台接收
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程