回答:
toscho:有三種實現方式:偽元素(pseudo-elements)、偽類( pseudo-classes)和Notihing。
WebKit和Blink(Safari,Google Chrome, Opera15+)使用偽元素
::-webkit-input-placeholder
Mozilla Firefox 4-18使用偽類
:-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的字體顏色均為紅色。所有樣式都要針對不同的選擇器而定,不要打包整體處理,因為其中一個出問題,其他的都會失效。
*::-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; }
還有一種好辦法:
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; }
最后一種是從網上找的:
$('[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;"/>
<input type="text" value="placeholder text" onfocus="if(!this.haswriting){this.style.color='#000'; this.value='';}" onblur="if(!this.value){this.style.color='#f00'; this.value='placeholder text'; this.haswriting=false;}else{this.haswriting=true};" style="color: #f00;"/>
雖然完善了部分,但用戶提交時會將placeholder text跟隨表單一同提交到服務器!
我選了這個:
::-webkit-input-placeholder { /* WebKit browsers */ color: #909; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #909; opacity: 1; } ::-moz-placeholder { /* Mozilla Firefox 19+ */ color: #909; opacity: 1; } :-ms-input-placeholder { /* Internet Explorer 10+ */ color: #909; }
<input placeholder="Stack Snippets are awesome!">
opacity: 1;是為了修改FF的默認透明度導致顏色偏差;其它注意的是文本大小的設置和 input types (email, search). These might affect the rendering in unexpected ways. Use the properties -webkit-appearance and -moz-appearance to change that. 例子:
[type="search"] { -moz-appearance: textfield; -webkit-appearance: textfield; appearance: textfield; }