[英]Change color of textarea placeholder onclick
假設我有一個按鈕和一個文本區域。 textarea占位符的正常state是灰色的。 單擊按鈕時,它應該將占位符更改為黑色。 我該怎么做?
::placeholder
is the pseudo class that will control the placeholder text of an input... You can set this in a class and then toggle the class or add the class to your element on click.
let ta = document.getElementById('ta') function changeTAText(){ // the following would toggle on each click //=> ta.classList.toggle('placeHolderText') // The following would add it without the // ability to toggle on click ta.classList = 'placeHolderText'; } btn.addEventListener("click", changeTAText)
.placeHolderText::placeholder { color: black; }
<textarea id="ta" placeholder="enter your text here"></textarea> <button id="btn">Change Color of Placeholder Text</button>
我將發布部分代碼以使占位符變為紅色。 添加一個按鈕並在單擊按鈕時切換 textarea placeholder-red
的 class 。
textarea.placeholder-red::placeholder { color: red; }
<textarea rows="4" cols="50" class="placeholder-red" placeholder="Describe yourself here..."></textarea>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.