简体   繁体   English

使用 type=“color” 从输入中选择颜色时如何更改身体的背景

[英]How do I change the background of my body when picking a color from my input with the type=“color”

I have tried adding a onclick event that when clicked changes the background to whatever is set in my input field but it did not work.我尝试添加一个 onclick 事件,当单击该事件时,该事件将背景更改为我输入字段中设置的任何内容,但它不起作用。 I do not know if I am using a wrong event or if it is something else.我不知道我是否使用了错误的事件,或者它是否是其他东西。 Can someone please help me thank you.有人可以帮我吗谢谢。

 let color = document.getElementById("color-picker"); color.addEventListener("click", function() {colorPicker(color)}); function colorPicker(chooseColor) { body.style.backgroundColor = chooseColor }
 <fieldset> <legend>Appearing here</legend> <div> <label for="color-picker">Colors</label> </div> <div> <input type="color" id="color-picker"> </div> </fieldset>

A few issues几个问题

  1. the change event should be used instead of click应该使用change事件而不是click
  2. should be document.body instead of body应该是document.body而不是body
  3. should use chooseColor.value to pick the value of the color picker应该使用chooseColor.value来选择颜色选择器的值
  1. event "input" would be much suitable for your intent.事件“输入”将非常适合您的意图。
  2. you passed the element to the colorPicker function so element's value has to be used , not element itself.您将元素传递给 colorPicker function 所以必须使用元素的值,而不是元素本身。
  3. document.body is correct, not just only body document.body是正确的,而不仅仅是 body

 let color = document.getElementById("color-picker"); color.addEventListener("input", function() {colorPicker(color)}); function colorPicker(chooseColor) { document.body.style.backgroundColor = chooseColor.value; }
 <fieldset> <legend>Appearing here</legend> <div> <label for="color-picker">Colors</label> </div> <div> <input type="color" id="color-picker"> </div> </fieldset>

You can simplify this by putting the addEventListener function all on one line (using an arrow => function, and using an onChange event, not onClick . Then you want to return color.value , not the input element color itself. Finally, you want to use document.body , not body : You can simplify this by putting the addEventListener function all on one line (using an arrow => function, and using an onChange event, not onClick . Then you want to return color.value , not the input element color itself. Finally, you want使用document.body ,而不是body

 let color = document.getElementById("color-picker") color.addEventListener("change",() => document.body.style.backgroundColor = color.value)
 <fieldset> <legend>Appearing here</legend> <div> <label for="color-picker">Colors</label> </div> <div> <input type="color" id="color-picker"> </div> </fieldset>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当范围类型的输入值更改时更改正文的背景颜色 - Change the background color of body when value of input with type of range is change 使用Bootstrap时如何改变身体的颜色? - How can I change the color of my body when using Bootstrap? 如何顺利更改背景颜色? - How do I change the color of my background smoothly? 当我输入并提交内容时,如何停止输入表单以更改背景颜色和文本颜色? - How do I stop the input form to change background color and text color when I input and submit something? 我如何让我的分区在点击时改变颜色? - How do I make my divisions to change color when they are clicked? 选中复选框后,如何更改DIV的颜色? - How do I change the color of my DIV when checkbox is checked? 单击交换按钮时,如何使红色div更改背景颜色以在红色和绿色之间切换? - How do I get my red div to change background color to toggle between red and green when I click on the swap button? 仅使用 Javascript 单击导航菜单选项时,如何使导航菜单选项更改颜色或背景? - How Do I make my Nav Menu option change color or background when I click on it using Javascript only? 如何更改输入类型范围的拇指的颜色? - How to change the color of the thumb of my input type range? 如何根据 onclick 更改输入字段背景颜色? - How do i change input field background color based on onclick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM