JavaScript 实现 HTML5 Placeholder 兼容旧版浏览器的方案

作者:流萤

时间:2026-10-26

浏览:593

HTML5 原生支持 input 和 textarea 的 placeholder 属性,但部分旧版浏览器如 IE 并不支持。本文介绍了一种通过 JavaScript 模拟实现 placeholder 效果的方法,利用绝对定位的 label 标签覆盖在输入框上,通过监听焦点事件控制显示与隐藏,从而在不支持原生属性的浏览器中提供一致的交互体验。

文章目录

JavaScript 实现 HTML5 Placeholder 兼容旧版浏览器的方案

HTML5 规范中原生支持 input 和 textarea 元素的 placeholder 属性,用于显示输入提示。然而,对于不支持该属性的旧版浏览器(例如 Internet Explorer),可以通过 JavaScript 模拟实现相同的功能。


js代码

JavaScript 实现代码

具体实现代码如下:

 
(function(){
//判断是否支持placeholder
function isPlaceholer(){
var input = document.createElement('input');
return "placeholder" in input;
} 
//不支持的代码
if(!isPlaceholer()){
//创建一个类
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集合转化为数组
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代码:

HTML 结构示例

代码如下:

 
<div>
<input type="text" placeholder="提示1" /><br>
<textarea placeholder="提示2" /></textarea><br>
<input type="text" placeholder="提示3" /><br>
</div>

css代码:

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;}

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章