js模仿html5 placeholder適應(yīng)于不支持的瀏覽器
js代碼
(function(){
//判斷是否支持placeholder
function isPlaceholer(){
var input = document.createElement('input');
return "placeholder" in input;
}
//不支持的代碼
if(!isPlaceholer()){
//創(chuàng)建一個(gè)類
function Placeholder(obj){
this.input = obj;
this.label = document.createElement('label');
this.label.innerHTML = obj.getAttribute('placeholder');
this.label.style.cssText = 'position:absolute; text-indent:4px;color:#999999; font-size:12px;';
if(obj.value != ''){
this.label.style.display = 'none';
}
this.init();
}
Placeholder.prototype = {
//取位置
getxy : function(obj){
var left, top;
if(document.documentElement.getBoundingClientRect){
var html = document.documentElement,
body = document.body,
pos = obj.getBoundingClientRect(),
st = html.scrollTop || body.scrollTop,
sl = html.scrollLeft || body.scrollLeft,
ct = html.clientTop || body.clientTop,
cl = html.clientLeft || body.clientLeft;
left = pos.left + sl - cl;
top = pos.top + st - ct;
}
else{
while(obj){
left += obj.offsetLeft;
top += obj.offsetTop;
obj = obj.offsetParent;
}
}
return{
left: left,
top : top
}
},
//取寬高
getwh : function(obj){
return {
w : obj.offsetWidth,
h : obj.offsetHeight
}
},
//添加寬高值方法
setStyles : function(obj,styles){
for(var p in styles){
obj.style[p] = styles[p]+'px';
}
},
init : function(){
var label = this.label,
input = this.input,
xy = this.getxy(input),
wh = this.getwh(input);
this.setStyles(label, {'width':wh.w, 'height':wh.h, 'lineHeight':20, 'left':xy.left, 'top':xy.top});
document.body.appendChild(label);
label.onclick = function(){
this.style.display = "none";
input.focus();
}
input.onfocus = function(){
label.style.display = "none";
};
input.onblur = function(){
if(this.value == ""){
label.style.display = "block";
}
};
}
}
var inpColl = document.getElementsByTagName('input'),
textColl = document.getElementsByTagName('textarea');
//html集合轉(zhuǎn)化為數(shù)組
function toArray(coll){
for(var i = 0, a = [], len = coll.length; i < len; i++){
a[i] = coll[i];
}
return a;
}
var inpArr = toArray(inpColl),
textArr = toArray(textColl),
placeholderArr = inpArr.concat(textArr);
for (var i = 0; i < placeholderArr.length; i++){
if (placeholderArr[i].getAttribute('placeholder')){
new Placeholder(placeholderArr[i]);
}
}
}
})()
html代碼:
<div>
<input type="text" placeholder="提示1" /><br>
<textarea placeholder="提示2" /></textarea><br>
<input type="text" placeholder="提示3" /><br>
</div>
css代碼:
div,input,textarea{ margin:0; padding:0;}
div{width:400px; margin:100px auto 0;}
input,textarea{width:200px;height:20px; margin-top:5px;line-height:20px;border:1px #666666 solid; background-color:#fff; padding-left:2px;}
textarea{ height:60px; font-size:12px; resize:none;}
相關(guān)文章
JavaScript+Canvas實(shí)現(xiàn)彩色圖片轉(zhuǎn)換成黑白圖片的方法分析
這篇文章主要介紹了JavaScript+Canvas實(shí)現(xiàn)彩色圖片轉(zhuǎn)換成黑白圖片的方法,結(jié)合實(shí)例形式分析了javascript結(jié)合HTML5相關(guān)函數(shù)修改頁(yè)面圖片元素顯示效果相關(guān)操作技巧,需要的朋友可以參考下2018-07-07javascript正則表達(dá)式之search()用法實(shí)例
這篇文章主要介紹了javascript正則表達(dá)式之search()用法,實(shí)例分析了search()的使用技巧,需要的朋友可以參考下2015-01-01javascript 表格內(nèi)容排序 簡(jiǎn)單操作示例代碼
本文為大家詳細(xì)介紹下javascript實(shí)現(xiàn)表格內(nèi)容排序,喜歡的朋友可以參考下2014-01-01JavaScript實(shí)現(xiàn)Java中StringBuffer的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)Java中StringBuffer的方法,實(shí)例分析了StringBuffer類的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-02-02Bootstrap源碼學(xué)習(xí)筆記之bootstrap進(jìn)度條
本文通過源碼給大家解析bootstrap進(jìn)度條樣式,分為條紋進(jìn)度條,動(dòng)態(tài)條紋進(jìn)度條,層疊進(jìn)度條和帶Label的進(jìn)度條,下面通過代碼給大家簡(jiǎn)單介紹下,感興趣的朋友一起看看吧2016-12-12javascript 學(xué)習(xí)筆記(onchange等)
javascript 學(xué)習(xí)筆記,一些簡(jiǎn)單的小技巧,學(xué)習(xí)js的朋友可以看下。2010-11-11微信端調(diào)取相冊(cè)和攝像頭功能,實(shí)現(xiàn)圖片上傳,并上傳到服務(wù)器
這篇文章主要介紹了微信端調(diào)取相冊(cè)和攝像頭功能圖片上傳服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Sample script that displays all of the users in a given SQL
Sample script that displays all of the users in a given SQL Server DB...2007-06-06