html颜色选择器

时间:2024-04-17 14:49:05

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>select color</title>
</head>
<body>
  <input type="color" name="color" id="color" onchange="changeColor()"> //选择颜色的标签,type="color"

  <span id="colorInfo"></span>
</body>
<script type="text/javascript">
  var color = document.getElementById("color");            //通过使用 getElementById() 来访问 <color> 元素
  var colorInfo = document.getElementById("colorInfo");
  colorInfo.style.color = color.value;                  //给<span>的字体加颜色
  colorInfo.innerHTML = color.value;                  //给<span>加内容(<color>的值)
  function changeColor(){                      //改变颜色的事件
    colorInfo.style.color = color.value;
    colorInfo.innerHTML = color.value;
  }
</script>
</html>