Chrome适用input=[type=text]占位文字特性,但以下CSS款式却不起功效:
CSS
input[placeholder], [placeholder], *[placeholder] {
color:red !important;
}
HTML input句子
<input type="text" placeholder="Value" />
运作結果值還是灰色,Color:red沒有功效。有甚么方式能够改动占位文字的色调吗?我在访问器里安裝了jQuery占位文字软件,但依然无用。(!important仅有IE7和firefox能鉴别)
回应:
toscho:有3种完成方法:伪元素(pseudo-elements)、伪类( pseudo-classes)和Notihing。
WebKit和Blink(Safari,Google Chrome, Opera15+)应用伪元素
::-webkit-input-placeholder
Mozilla Firefox 4⑴8应用伪类
:-moz-placeholder
Mozilla Firefox 19+ 应用伪元素
::-moz-placeholder
IE10应用伪类
:-ms-input-placeholder
IE9和Opera12下列版本号的CSS挑选器均不适用占位文字。必须留意的是伪元素在Shadow DOM里会起到元素的真正功效。
CSS挑选器
由于每一个访问器的CSS挑选器都有一定的差别,因此必须对于每一个访问器做独立的设置。
::-webkit-input-placeholder { /* WebKit browsers */
color: #999;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #999;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #999;
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
color: #999;
}
Matt:textareas(文字框可拉伸)设计风格款式的编码,以下:
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
color: #636363;
}
input:-moz-placeholder, textarea:-moz-placeholder {
color: #636363;
}
brillout.com:input和Textarea的字体样式色调均为鲜红色。全部款式都要对于不一样的挑选器而定,不必装包总体解决,由于在其中1个出难题,别的的都会无效。
*::-webkit-input-placeholder {
color: red;
}
*:-moz-placeholder {
color: red;
}
*:-ms-input-placeholder {
/* IE10+ */
color: red;
}
James Donnelly:在Firefox和IE里,一切正常input文字色调遮盖占位符色调的方式:
::-webkit-input-placeholder {
color: red; text-overflow: ellipsis;
}
:-moz-placeholder {
color: #acacac !important; text-overflow: ellipsis;
}
::-moz-placeholder {
color: #acacac !important; text-overflow: ellipsis;
} /* for the future */
:-ms-input-placeholder {
color: #acacac !important; text-overflow: ellipsis;
}
也有1种好方法:
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
color: #666;
}
input:-moz-placeholder, textarea:-moz-placeholder {
color: #666;
}
input::-moz-placeholder, textarea::-moz-placeholder {
color: #666;
}
input:-ms-input-placeholder, textarea:-ms-input-placeholder {
color: #666;
}
最终1种是从在网上找的:
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
这个编码启用的标准是,先载入Javascript再用CSS改动占位符特性。
form .placeholder {
color: #222;
font-size: 25px;
/* etc */
}
user1729061:无需CSS和占位文字,一样能获得同样实际效果。
input type="text" value="placeholder text" onfocus="this.style.color='#000';
this.value='';" style="color: #f00;"/>