微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解决IE不支持HTML5表单属性placeholder的问题

//解决IE不支持HTML5表单属性placeholder的问题
(function ($) {
    $.fn.placeholder = function (options) {
        var defaults = {
            pColor: "#ccc",pActive: "#999",pFont: "14px",activeBorder: "#080",posL: 44,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 = $("<label>",{
                "class": "placeholderCss","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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。