相信您需要供应商前缀(来自 css-tricks.com):
::-webkit-input-placeholder {
color: red;
}
:-moz-placeholder { /* Firefox 18- */
color: red;
}
::-moz-placeholder { /* Firefox 19+ */
color: red;
}
:-ms-input-placeholder {
color: red;
}
使用 javascript,您只能以编程方式在焦点事件上应用类似的样式(使用供应商前缀)。
编辑:事实上,我认为这些样式不能使用 javascript 应用。您需要创建一个类并使用 js 应用它。
CSS:
input.placeholderred::-webkit-input-placeholder {
color: red;
}
jQuery:
var $textInput = $('#TextField1');
$textInput.on('focusin',
function () {
$(this).addClass('placeholderred');
}
);
$textInput.on('focusout',
function () {
$(this).removeClass('placeholderred');
}
);
JS:
var textInput = document.getElementById('TextField1');
textInput.addEventListener('focus',
function () {
this.classList.add('placeholderred');
}
);
textInput.addEventListener('blur',
function () {
this.classList.remove('placeholderred');
}
);
感谢最有帮助的 Armfoot,小提琴:http://jsfiddle.net/qbkkabra/2/