html的placeholder为表单,IE不支持HTML5表单属性placeholder的解决办法

1. [代码][JavaScript]代码

(function ($) {

$.fn.placeholder = function (options) {

var defaults = {

pColor: "#ccc",

pActive: "#999",

pFont: "14px",

activeBorder: "#080",

posL: 8,

zIndex: "99"

},

opts = $.extend(defaults, options);

//

return this.each(function () {

if ("placeholder" in document.createElement("input")) return;

$(this).parent().css("position", "relative");

var isIE = $.browser.msie,

version = $.browser.version;

//不支持placeholder的浏览器

var $this = $(this),

msg = $this.attr("placeholder"),

iH = $this.outerHeight(),

iW = $this.outerWidth(),

iX = $this.position().left,

iY = $this.position().top,

oInput = $("", {

"class": "test",

"text": msg,

"css": {

"position": "absolute",

"left": iX + "px",

"top": iY + "px",

"width": iW - opts.posL + "px",

"padding-left": opts.posL + "px",

"height": iH + "px",

"line-height": iH + "px",

"color": opts.pColor,

"font-size": opts.pFont,

"z-index": opts.zIndex,

"cursor": "text"

}

}).insertBefore($this);

//初始状态就有内容

var value = $this.val();

if (value.length > 0) {

oInput.hide();

};

//

$this.on("focus", function () {

var value = $(this).val();

if (value.length > 0) {

oInput.hide();

}

oInput.css("color", opts.pActive);

//

if(isIE && version < 9){

var myEvent = "propertychange";

}else{

var myEvent = "input";

}

$(this).on(myEvent, function () {

var value = $(this).val();

if (value.length == 0) {

oInput.show();

} else {

oInput.hide();

}

});

}).on("blur", function () {

var value = $(this).val();

if (value.length == 0) {

oInput.css("color", opts.pColor).show();

}

});

//

oInput.on("click", function () {

$this.trigger("focus");

$(this).css("color", opts.pActive)

});

//

$this.filter(":focus").trigger("focus");

});

}

})(jQuery)

2. [代码]调用方法:

//解决IE下不支持placeholder

if($.browser.msie) {

$(":input[placeholder]").each(function(){

$(this).placeholder();

});

}

阅读(256) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值