最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Get ::placeholder color using Javascript - Stack Overflow

programmeradmin3浏览0评论

Is it possible to get current color value of an input placeholder text? Usually I'd use getComputedStyle to get current element style values, but it does not return the placeholder value.

Any workarounds for this apart from parsing the css text and getting the value using regex?

Thanks.

Is it possible to get current color value of an input placeholder text? Usually I'd use getComputedStyle to get current element style values, but it does not return the placeholder value.

Any workarounds for this apart from parsing the css text and getting the value using regex?

Thanks.

Share Improve this question edited May 24, 2016 at 10:13 Jakub Matczak 15.7k5 gold badges51 silver badges67 bronze badges asked May 24, 2016 at 9:58 SgoldySgoldy 7964 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

the second parameter of getComputedStyle is pseudoElt selector

so to get placeholder color of your input element:

var styles = window.getComputedStyle(inputEl, ':placeholder');
console.log(styles.getPropertyValue('color'));

It's

window.getComputedStyle(inputEl, '::placeholder').getPropertyValue("color");
// note the DOUBLE colon here ----^^

Works in both Chrome and Firefox

Usually placeholder we have to use a class name and then change the placeholder color

input.text-filed::-webkit-input-placeholder {
   color: red;
}

So u know the class which is have the color

发布评论

评论列表(0)

  1. 暂无评论