HTML5 input placeholder 顏色修改示例
發(fā)布時(shí)間:2014-05-30 16:54:51 作者:佚名
我要評論

這篇文章主要介紹了有關(guān)HTML5 input placeholder 顏色修改方面的知識,需要的朋友可以參考下
Chrome支持input=[type=text]占位文本屬性,但下列CSS樣式卻不起作用:
CSS
input[placeholder], [placeholder], *[placeholder] {
color:red !important;
}
HTML input語句
<input type="text" placeholder="Value" />
運(yùn)行結(jié)果值還是灰色,Color:red沒有作用。有什么方法可以修改占位文本的顏色嗎?我在瀏覽器里安裝了jQuery占位文本插件,但仍然無用。(!important只有IE7和firefox能識別)
回答:
toscho:有三種實(shí)現(xiàn)方式:偽元素(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里會起到元素的真實(shí)作用。
CSS選擇器
因?yàn)槊總€(gè)瀏覽器的CSS選擇器都有所差異,所以需要針對每個(gè)瀏覽器做單獨(dú)的設(shè)定。
::-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(文本框可拉伸)風(fēng)格樣式的代碼,如下:
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
color: #636363;
}
input:-moz-placeholder, textarea:-moz-placeholder {
color: #636363;
}
brillout.com:input和Textarea的字體顏色均為紅色。所有樣式都要針對不同的選擇器而定,不要打包整體處理,因?yàn)槠渲幸粋€(gè)出問題,其他的都會失效。
*::-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;
}
最后一種是從網(wǎng)上找的:
$('[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('');
}
})
});
這個(gè)代碼調(diào)用的規(guī)則是,先加載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;"/>
CSS
復(fù)制代碼
代碼如下:input[placeholder], [placeholder], *[placeholder] {
color:red !important;
}
HTML input語句
復(fù)制代碼
代碼如下:<input type="text" placeholder="Value" />
運(yùn)行結(jié)果值還是灰色,Color:red沒有作用。有什么方法可以修改占位文本的顏色嗎?我在瀏覽器里安裝了jQuery占位文本插件,但仍然無用。(!important只有IE7和firefox能識別)
回答:
toscho:有三種實(shí)現(xiàn)方式:偽元素(pseudo-elements)、偽類( pseudo-classes)和Notihing。
WebKit和Blink(Safari,Google Chrome, Opera15+)使用偽元素
復(fù)制代碼
代碼如下:::-webkit-input-placeholder
Mozilla Firefox 4-18使用偽類
復(fù)制代碼
代碼如下::-moz-placeholder
Mozilla Firefox 19+ 使用偽元素
復(fù)制代碼
代碼如下:::-moz-placeholder
IE10使用偽類
復(fù)制代碼
代碼如下::-ms-input-placeholder
IE9和Opera12以下版本的CSS選擇器均不支持占位文本。需要注意的是偽元素在Shadow DOM里會起到元素的真實(shí)作用。
CSS選擇器
因?yàn)槊總€(gè)瀏覽器的CSS選擇器都有所差異,所以需要針對每個(gè)瀏覽器做單獨(dú)的設(shè)定。
復(fù)制代碼
代碼如下:::-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(文本框可拉伸)風(fēng)格樣式的代碼,如下:
復(fù)制代碼
代碼如下:input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
color: #636363;
}
input:-moz-placeholder, textarea:-moz-placeholder {
color: #636363;
}
brillout.com:input和Textarea的字體顏色均為紅色。所有樣式都要針對不同的選擇器而定,不要打包整體處理,因?yàn)槠渲幸粋€(gè)出問題,其他的都會失效。
復(fù)制代碼
代碼如下:*::-webkit-input-placeholder {
color: red;
}
*:-moz-placeholder {
color: red;
}
*:-ms-input-placeholder {
/* IE10+ */
color: red;
}
James Donnelly:在Firefox和IE里,正常input文本顏色覆蓋占位符顏色的方法:
復(fù)制代碼
代碼如下:::-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;
}
還有一種好辦法:
復(fù)制代碼
代碼如下: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;
}
最后一種是從網(wǎng)上找的:
復(fù)制代碼
代碼如下:$('[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('');
}
})
});
這個(gè)代碼調(diào)用的規(guī)則是,先加載Javascript再用CSS修改占位符屬性。
復(fù)制代碼
代碼如下:form .placeholder {
color: #222;
font-size: 25px;
/* etc */
}
user1729061:不用CSS和占位文本,同樣能得到相同效果。
復(fù)制代碼
代碼如下:input type="text" value="placeholder text" onfocus="this.style.color='#000';
this.value='';" style="color: #f00;"/>
相關(guān)文章
- 這篇文章主要介紹了HTML5新增的8類INPUT輸入類型介紹,這8個(gè)新的輸入類型分別是email類型、url類型、number類型、range類型、日期類型、search類型、tel類型,本文分別對它們2015-07-06
- 有時(shí)候?yàn)g覽器有很多緩存,我們就執(zhí)行不了某種操作,必須要清除緩存才可以運(yùn)行,其實(shí)多數(shù)瀏覽器默認(rèn)會緩存input的值,只有使用ctl+F5強(qiáng)制刷新的才可以清除緩存記錄。如果不2014-11-11
- 前的工作要完成,記錄一個(gè)html5中的input新屬性,range。發(fā)現(xiàn)這個(gè)屬性是在一個(gè)網(wǎng)站的調(diào)色器中,感覺確實(shí)比較形象化,好看,喜歡的朋友可以看看2014-09-05
HTML 5 input placeholder 屬性如何完美兼任ie
這篇文章主要介紹了HTML 5 input placeholder 屬性完美兼任ie的方法,需要的朋友可以參考下2014-05-12- HTML5改進(jìn)的地方想必大家有所知曉,下面我要介紹的是兩個(gè)新的input元素類型email和url。讓我們跟著代碼來看看他們的好處,感興趣的朋友可以參考下2013-08-13
- 今天我剛接觸html+css3感覺還不錯(cuò),下面為大家介紹下用到的input 屬性,感興趣的朋友可以看下截圖哈,希望對大家有所幫助2013-06-28
html4和html5區(qū)別之如何在一個(gè)input上添加焦點(diǎn)實(shí)現(xiàn)代碼
如何在一個(gè)input上添加焦點(diǎn),有很多的實(shí)現(xiàn)方法,本文分別用html4和html5做了下演示,感興趣的朋友可以參考下,或許本文對你有所幫助2013-02-07HTML5 input新增type屬性color顏色拾取器的實(shí)例代碼
type 屬性規(guī)定 input 元素的類型。本文較詳細(xì)的給大家介紹了HTML5 input新增type屬性color顏色拾取器的實(shí)例代碼,感興趣的朋友跟隨腳本之家小編一起看看吧2018-08-27