IE下实现placeholder效果的jquery插件

时间:2022-12-01 18:09:00
$(document)
.ready(
function() {
var doc = document, inputs = doc
.getElementsByTagName('input'), supportPlaceholder = 'placeholder' in doc
.createElement('input'), placeholder = function(
input) {
var text = input.getAttribute('placeholder'), defaultValue = input.defaultValue;
if (defaultValue == '') {
input.value = text
input.setAttribute("old_color", input.style.color);
input.style.color = "#c0c0c0";
}
input.onfocus = function() {
this.style.color = this.getAttribute("old_color");
if (input.value === text) {
this.value = ''
}
};
input.onblur = function() {
if (input.value === '') {
this.style.color = "#c0c0c0";
this.value = text
}
}
};
if (!supportPlaceholder) {
for ( var i = 0, len = inputs.length; i < len; i++) {
var input = inputs[i], text = input
.getAttribute('placeholder');
if (input.type === 'text' && text) {
placeholder(input)
}
}
}
});
直接把代码复制下来,保存成一个js文件引用即可,根本不用再做任何处理,超级方便~