jquery 解决ie9及以下版本不支持placeholder属性

时间:2022-07-09 20:36:52
//解决ie9及以下版本不支持placeholder属性
$.fn.placeholder = function(options) {
var opts = $.extend({}, $.fn.placeholder.defaults, options);
var isIE = document.all ? true : false;
return this.each(function() {
var _this = this,
placeholderValue = _this.getAttribute("placeholder"); //缓存默认的placeholder
if (isIE) {
_this.setAttribute("value", placeholderValue);
_this.onfocus = function() {
$.trim(_this.value) == placeholderValue ? _this.value = "" : '';
};
_this.onblur = function() {
$.trim(_this.value) == "" ? _this.value = placeholderValue : '';
};
}
});
};