【JS】IE兼容placeholder

时间:2022-09-16 08:18:02

直接上代码:

   $(document).ready(function () {
var doc = document,
textareas = doc.getElementsByTagName('textarea'),
supportPlaceholder = 'placeholder' in doc.createElement('textarea'), placeholder = function (textarea) {
var text = textarea.getAttribute('placeholder'),
defaultValue = textarea.defaultValue;
if (defaultValue == '') {
textarea.value = text
}
textarea.onfocus = function () {
if (textarea.value === text) {
this.value = ''
}
};
textarea.onblur = function () {
if (textarea.value === '') {
this.value = text
}
}
}; if (!supportPlaceholder) {
for (var i = 0, len = textareas.length; i < len; i++) {
var textarea = textareas[i],
text = textarea.getAttribute('placeholder');
if ( text) {
placeholder(textarea);
}
}
}
});

textarea可以换成input