js類比placeholder效果1

來源:互聯網
上載者:User

標籤:placeholder

由於有的瀏覽器不支援placeholder,所以寫了一個jquery外掛程式來類比placeholder效果。

使用方法:$("#IDName").placeholder();

話不多說,直接上代碼~~~

(function ($) {    $.fn.placeholder = function (options) {        var defaults = {            pColor: "#BEBEBE",            pActive: "#999",            pFont: "14px",            activeBorder: "#080",            posL: 8,            zIndex: "99"        },        opts = $.extend(defaults, options);        $(".test").remove();        return this.each(function () {            if ("placeholder" in document.createElement("input")) return;            $(this).parent().css("position", "relative");//            var isIE = $.browser.msie,//            version = $.browser.version;            if(!$(this).is(":hidden")){//不支援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 () {                    oInput.hide();                    oInput.css("color", opts.pActive);                }).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);


js類比placeholder效果1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.