现在的位置: 首页 > 综合 > 正文

placeholder 兼容处理

2014年09月05日 ⁄ 综合 ⁄ 共 525字 ⁄ 字号 评论关闭

placeholder是html5 的属性,一些不支持的ie就苦逼了,只能用js处理,代码如下:

(function($) {
	$.fn.extend({
		placeholder: function() {
			if ("placeholder" in document.createElement("input")) {
				return this //如果原生支持placeholder属性,则返回对象本身
			} else {
				return this.each(function() {
					var _this = $(this);
					_this.val(_this.attr("placeholder")).focus(function() {
						if (_this.val() === _this.attr("placeholder")) {
							_this.val("")
						}
					}).blur(function() {
						if (_this.val().length === 0) {
							_this.val(_this.attr("placeholder"))
						}
					})
				})
			}
		}
	})
})(jQuery);

这个不支持密码输入框,还有修改颜色。

可以点击这里研究一下一个比较好的插件(jquery.placehold.js)。

也可以查看这些博客。文章1文章2


抱歉!评论已关闭.