js模仿html5placeholder适应于不支持的浏览器
2023-06-13 09:14:44 时间
html5原生支持placeholder,对于不支持的浏览器(ie),可用js模拟实现。
js代码
复制代码代码如下:
js代码
(function(){
//判断是否支持placeholder
functionisPlaceholer(){
varinput=document.createElement("input");
return"placeholder"ininput;
}
//不支持的代码
if(!isPlaceholer()){
//创建一个类
functionPlaceholder(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){
varleft,top;
if(document.documentElement.getBoundingClientRect){
varhtml=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(varpinstyles){
obj.style[p]=styles[p]+"px";
}
},
init:function(){
varlabel=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";
}
};
}
}
varinpColl=document.getElementsByTagName("input"),
textColl=document.getElementsByTagName("textarea");
//html集合转化为数组
functiontoArray(coll){
for(vari=0,a=[],len=coll.length;i<len;i++){
a[i]=coll[i];
}
returna;
}
varinpArr=toArray(inpColl),
textArr=toArray(textColl),
placeholderArr=inpArr.concat(textArr);
for(vari=0;i<placeholderArr.length;i++){
if(placeholderArr[i].getAttribute("placeholder")){
newPlaceholder(placeholderArr[i]);
}
}
}
})()
html代码:
<div>
<inputtype="text"placeholder="提示1"/><br>
<textareaplaceholder="提示2"/></textarea><br>
<inputtype="text"placeholder="提示3"/><br>
</div>
css代码:
div,input,textarea{margin:0;padding:0;}
div{width:400px;margin:100pxauto0;}
input,textarea{width:200px;height:20px;margin-top:5px;line-height:20px;border:1px#666666solid;background-color:#fff;padding-left:2px;}
textarea{height:60px;font-size:12px;resize:none;}
相关文章
- php清除浏览器缓存代码,js清除浏览器缓存
- Js如何防止页面后退(使浏览器后退按钮失效)[通俗易懂]
- nuxt.js 项目打包上线
- js定时器与延时器_JS做定时器倒计时
- cocos2dx lua热更新_cocos2d js
- js数组添加删除数据_如何删除数组中的元素
- js基础查漏补缺(更新)详解编程语言
- 使用JS技术实现Oracle数据库链接(js 链接 oracle)
- Redis中的订阅机制及其在JS中的应用(redis 订阅 js)
- jb51站长推荐的用js实现的多浏览器支持的图片轮换展示效果ie,firefox
- 初学JS的的小例子javascriptreplace高亮替换
- JS+CSS实现的一种交互体验表单页面
- js中过滤特殊字符的正则表达式
- 用js获取电脑信息(是使用与IE浏览器)
- JS网页播放声音实现代码兼容各种浏览器
- js判断上传文件大小及格式代码
- js在输入框屏蔽按键,只能键入数字的示例代码
- JS中的数组的sort方法使用示例
- JS判断浏览器是否支持某一个CSS3属性的方法
- js继承call()和apply()方法总结
- AngularJS+Node.js+MongoDB开发的基于高德地图位置的通讯录
- js判断浏览器版本以及浏览器内核的方法
- Windows下使用apache模块实现合并多个js、css提高网页加载速度
- js实现图片在未加载完成前显示加载中字样
- js自动生成的元素与页面原有元素发生堆叠的解决方法