- HTML: HyperText Markup Language | MDN"> - HTML: HyperText Markup Language | MDN"> - HTML: HyperText Markup Language | MDN
Có thể bạn quan tâm
First, there's some setup. Here we establish some variables, setting up a variable that contains the color we'll set the color picker to when we first load up, and then setting up a load handler to do the main startup work once the page is fully loaded.
jslet colorPicker; const defaultColor = "#0000ff"; window.addEventListener("load", startup, false);Initialization
Once the page is loaded, our load event handler, startup(), is called:
jsfunction startup() { colorPicker = document.querySelector("#color-picker"); colorPicker.value = defaultColor; colorPicker.addEventListener("input", updateFirst, false); colorPicker.addEventListener("change", updateAll, false); colorPicker.select(); }This gets a reference to the color <input> element in a variable called colorPicker, then sets the color input's value to the value in defaultColor. Then the color input's input event is set up to call our updateFirst() function, and the change event is set to call updateAll(). These are both seen below.
Finally, we call select() to select the text content of the color input if the control is implemented as a text field (this has no effect if a color picker interface is provided instead).
Reacting to color changes
We provide two functions that deal with color changes. The updateFirst() function is called in response to the input event. It changes the color of the first paragraph element in the document to match the new value of the color input. Since input events are fired every time an adjustment is made to the value (for example, if the brightness of the color is increased), these will happen repeatedly as the color picker is used.
jsfunction updateFirst(event) { const p = document.querySelector("p"); if (p) { p.style.color = event.target.value; } }When the color picker is dismissed, indicating that the value will not change again (unless the user re-opens the color picker), a change event is sent to the element. We handle that event using the updateAll() function, using Event.target.value to obtain the final selected color:
jsfunction updateAll(event) { document.querySelectorAll("p").forEach((p) => { p.style.color = event.target.value; }); }This sets the color of every <p> block so that its color attribute matches the current value of the color input, which is referred to using event.target.
Từ khóa » Html Color Input
-
HTML Input Type="color" - W3Schools
-
Input Type Color - With Color Picker - Tryit Editor V3.7
-
HTML Input Type="color" Attribute - Dofactory
-
- GeeksforGeeks">HTML | - GeeksforGeeks
-
How To Add A Color Picker In HTML
-
Color Input Type | Can I Use... Support Tables For HTML5, CSS3, Etc
-
How To Style Color Picker Window In An Input[type=color]?
-
HTML DOM Input Color Object - W3Schools
-
Color Picker - HTML Color Codes
-
Color Type - HTML5 Forms - Wufoo
-
- CodeProject Reference">Html - CodeProject Reference
-
Input[type="color"] Element (html)
-
How To Use Input Type Field With The Color Picker In HTML?