jQuery placeholder插件

之前公司有這樣的效果,placeholder和之前那種js模擬value的有一些區別,之前的那種都是在input文本框得到焦點的時候就消失了,而 html5引入的placeholder則不是,當得到焦點的時候是不消失的只有在你輸入內容的時候才消失,固然由於一些低級瀏覽器不支持這個屬性,固然 表明是IE瀏覽器,貌似IE9還不支持呢,其是模擬的思路也是比較簡單的,就是在不支持的瀏覽器上建立一個label來模擬placeholder,具體 的你們能夠看看源代碼,由於說很蒼白,看了代碼就都明白了,固然難點多是在於兩個方法,一個是oninut和onpropertychange,固然你 能夠開定時器來獲取,固然那樣我認爲性能太差,可是兼容性最好~好了不說直接上代碼了~ css

;(function ($) {
    $.fn.placeholder = function (options) {
        var defaults = {
            pColor: "#333",
            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 = $("<label>", {
                "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);
相關文章
相關標籤/搜索