您的位置:首页 > 其它

placeholder 兼容处理

2014-06-27 17:55 197 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: