HTML5 INPUT placeholder及兼容性處理_XHTML教程
由于placeholder是個(gè)新增屬性,目前只有少數(shù)瀏覽器支持,如何檢測(cè)瀏覽器是否支持它呢?(更多HTML5/CSS3特性檢測(cè)可以訪問(wèn))
function hasPlaceholderSupport() {
return 'placeholder' in document.createElement('input');
}
默認(rèn)提示文字是灰色的,可以通過(guò)CSS來(lái)改變文字樣式:
/* all */
::-webkit-input-placeholder { color:#f00; }
input:-moz-placeholder { color:#f00; }
/* individual: webkit */
#field2::-webkit-input-placeholder { color:#00f; }
#field3::-webkit-input-placeholder { color:#090; background:lightgreen; text-transform:uppercase; }
#field4::-webkit-input-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; }
/* individual: mozilla */
#field2:-moz-placeholder { color:#00f; }
#field3:-moz-placeholder { color:#090; background:lightgreen; text-transform:uppercase; }
#field4:-moz-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; }
兼容其他不支持placeholder的瀏覽器:
var PlaceHolder = {
_support: (function() {
return 'placeholder' in document.createElement('input');
})(),
//提示文字的樣式,需要在頁(yè)面中其他位置定義
className: 'abc',
init: function() {
if (!PlaceHolder._support) {
//未對(duì)textarea處理,需要的自己加上
var inputs = document.getElementsByTagName('input');
PlaceHolder.create(inputs);
}
},
create: function(inputs) {
var input;
if (!inputs.length) {
inputs = [inputs];
}
for (var i = 0, length = inputs.length; i <length; i++) {
input = inputs[i];
if (!PlaceHolder._support && input.attributes && input.attributes.placeholder) {
PlaceHolder._setValue(input);
input.addEventListener('focus', function(e) {
if (this.value === this.attributes.placeholder.nodeValue) {
this.value = '';
this.className = '';
}
}, false);
input.addEventListener('blur', function(e) {
if (this.value === '') {
PlaceHolder._setValue(this);
}
}, false);
}
}
},
_setValue: function(input) {
input.value = input.attributes.placeholder.nodeValue;
input.className = PlaceHolder.className;
}
};
//頁(yè)面初始化時(shí)對(duì)所有input做初始化
//PlaceHolder.init();
//或者單獨(dú)設(shè)置某個(gè)元素
//PlaceHolder.create(document.getElementById('t1'));
- HTML表單里的Label標(biāo)簽
- 淺析HTML與javascript中常用編碼
- CSS網(wǎng)頁(yè)布局中必須要了解的幾個(gè)XHTML標(biāo)簽
- title和alt 標(biāo)簽介紹及其異同
- 淺析HTML標(biāo)簽strong和em的區(qū)別
- 說(shuō)說(shuō)XHTML中的alt屬性和title屬性
- Semantics語(yǔ)義是Html Xhtml是否真正符合標(biāo)準(zhǔn)的重要部分
- 九個(gè)W3C XHTML1.0及CSS標(biāo)準(zhǔn)認(rèn)證需要注意的問(wèn)題
- 請(qǐng)注意頁(yè)面head區(qū)域的編碼是不是規(guī)范
- HTML validate - 驗(yàn)證HTML代碼穿越多重瀏覽器標(biāo)準(zhǔn)
- XHTML具有語(yǔ)義的標(biāo)簽:有關(guān)H1位置的討論
- HTML結(jié)構(gòu)更加清晰、規(guī)范,學(xué)習(xí)HTML5優(yōu)化結(jié)構(gòu)的思路。
- 相關(guān)鏈接:
- 教程說(shuō)明:
XHTML教程-HTML5 INPUT placeholder及兼容性處理
。