时间:2022-01-30 11:22:03 | 栏目:JavaScript代码 | 点击:次
本文实例为大家分享了JavaScript实现颜色查看器的具体代码,供大家参考,具体内容如下
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <title>颜色查看器</title> <style> #color { width: 150px; height: 150px; background-color: #fff; border: 1px solid #000; } </style> </head> <body> <div id="color"></div> <input id="inp" type="text" placeholder="请输入颜色代码..." /> <button id="trans">查看颜色</button> <button id="rst">复原</button> </body> <script> let trans = document.getElementById('trans'); let color = document.getElementById('color'); let inp = document.getElementById('inp'); let rst = document.getElementById('rst'); trans.addEventListener('click', () => { color.style.backgroundColor = inp.value; }); rst.addEventListener('click', () => { color.style.backgroundColor = '#fff'; inp.value = ''; }); </script> </html>