- 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 Text Javascript
-
Change Text Color On Click Using JavaScript | Bobbyhadz
-
JavaScript String Fontcolor() Method - W3Schools
-
HTML DOM Style Color Property - W3Schools
-
Change Text Color With Javascript? - Stack Overflow
-
How To Change The Font Color Of A Text Using JavaScript?
-
How To Change Font Color Of HTML Element In JavaScript?
-
How To Change The Text Color In JavaScript - Reactgo
-
Text Color Change In Js Code Example - Code Grepper
-
JavaScript: String Fontcolor() Method - TechOnTheNet
-
ntcolor() - JavaScript - MDN Web Docs
-
How To Change Text Color In HTML? - Top 3 Ways To Create Awesome ...
-
How To Change Text Color In Html - Javatpoint
-
How To Change Text Color On Click Using JavaScript?
-
How To Change Text Color In Javascript - YouTube