admin管理员组文章数量:1334887
I would like to style placeholder text in different color for each text field.
The code from mozilla doc below will affect entire input tag.
/%3a-moz-placeholder
<style type="text/css">
input:-moz-placeholder {
color: green;
}
</style>
I want to have different colors for each text field.
HTML
<input id="foo" type="text" placeholder="im green" />
<input id="foo2" type="text" placeholder="im red />
jQuery
I tried below but it will just set the color to the input text field itself not to the placeholder.
var elementId = "#foo2";
$(elementId + ":-moz-placeholder").css("color", "red");
I think my selector is not specified correctly though not sure how to set properly.
How do I style individual placeholder by element id using jQuery?
I would like to style placeholder text in different color for each text field.
The code from mozilla doc below will affect entire input tag.
https://developer.mozilla/en/CSS/%3a-moz-placeholder
<style type="text/css">
input:-moz-placeholder {
color: green;
}
</style>
I want to have different colors for each text field.
HTML
<input id="foo" type="text" placeholder="im green" />
<input id="foo2" type="text" placeholder="im red />
jQuery
I tried below but it will just set the color to the input text field itself not to the placeholder.
var elementId = "#foo2";
$(elementId + ":-moz-placeholder").css("color", "red");
I think my selector is not specified correctly though not sure how to set properly.
How do I style individual placeholder by element id using jQuery?
Share Improve this question edited Feb 17, 2012 at 4:07 Meow asked Feb 17, 2012 at 3:19 MeowMeow 19.2k52 gold badges143 silver badges183 bronze badges3 Answers
Reset to default 3Have you tried... CSS classes by any chance?
<input class="green" id="foo" type="text" placeholder="im green" />
<input class="red" id="foo2" type="text" placeholder="im red />
<style type="text/css">
input.green:-moz-placeholder {
color: green;
}
</style>
:-moz-placeholder is a pseudo class.
Pseudo classes are not part of the DOM, so jQuery can not directly edit anything about them.
An alternate approach would be to use classes to change colors, which is what Walkerneo suggested.
Take his code, and then use this to switch the class from green to red:
$("input#foo").toggleClass('red').toggleClass('green');
It's possible to prepend a block with jQuery:
var $inlineStyleTemplate = $("" +
"<style id='custom-menu-color'> " +
".navigation-md-lg > li > a, .header-right > ul > li > a,.header-right .fa-search {color: " + menuColor + "}" +
"svg {fill: " + menuColor + "}" +
"#search-search {border-color: " + menuColor + "}" +
"#search-search::-webkit-input-placeholder { color: " + menuColor + "}" +
"#search-search::-moz-placeholder { color: " + menuColor + "}" +
"#search-search:-ms-input-placeholder { color: " + menuColor + "}" +
"#search-search:-moz-placeholder { color: " + menuColor + "}" +
"</style>"
);
$('head').prepend($inlineStyleTemplate);
本文标签: javascriptHow to style placeholder text color for individual text fieldStack Overflow
版权声明:本文标题:javascript - How to style placeholder text color for individual text field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742323543a2453272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论