久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

HTML5 占位符在焦點上消失

HTML5 placeholder disappears on focus(HTML5 占位符在焦點上消失)
本文介紹了HTML5 占位符在焦點上消失的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否有免費的 jQuery 插件可以改變 placeholder 的行為以匹配 HTML5 規范?

聚焦前

專注于良好(Safari)

專注不好(Chrome、Firefox)

您可以通過這個簡單的小提琴 寫了一個很好的 jQuery 插件,就是這樣做的.
它比 Robert 的更穩定,并且在聚焦區域時會逐漸變為淺灰色.

  • 查看演示頁面
  • 在 GitHub
  • 上獲取它
  • 玩小提琴
<小時>

我修改了他的插件以讀取 placeholder 屬性,而不是手動創建 span.
這個小提琴有完整的代碼:

HTML

<input type="text" placeholder="Hello, world!">

JS

//Stefano J. Attardi 的原始代碼,MIT 許可證(函數($){功能切換標簽(){var 輸入 = $(this);if (!input.parent().hasClass('placeholder')) {var label = $('<label>').addClass('placeholder');input.wrap(標簽);var span = $('<span>');span.text(input.attr('占位符'))input.removeAttr('占位符');span.insertBefore(輸入);}設置超時(函數(){var def = input.attr('title');if (!input.val() || (input.val() == def)) {input.prev('span').css('visibility', '');如果(定義){var dummy = $('<label></label>').text(def).css('visibility','hidden').appendTo('body');input.prev('span').css('margin-left', dummy.width() + 3 + 'px');dummy.remove();}} 別的 {input.prev('span').css('visibility', 'hidden');}}, 0);};函數重置字段(){var def = $(this).attr('title');if (!$(this).val() || ($(this).val() == def)) {$(this).val(def);$(this).prev('span').css('visibility', '');}};var fields = $('input, textarea');fields.live('mouseup', toggleLabel);//IE 重置圖標需要 [X]fields.live('keydown', toggleLabel);fields.live('paste', toggleLabel);fields.live('focusin', function() {$(this).prev('span').css('color', '#ccc');});fields.live('focusout', function() {$(this).prev('span').css('color', '#999');});$(函數(){$('input[placeholder], textarea[placeholder]').each(函數() { toggleLabel.call(這個);});});})(jQuery);

CSS

.placeholder {背景:白色;向左飄浮;明確:兩者;}.占位符跨度{位置:絕對;填充:5px;左邊距:3px;顏色:#999;}.placeholder 輸入,.placeholder textarea {位置:相對;邊距:0;邊框寬度:1px;填充:6px;背景:透明;字體:繼承;}/* 刪除 Safari 的額外填充.如果您不關心像素完美,請刪除.*/@media 屏幕和 (-webkit-min-device-pixel-ratio:0) {.placeholder 輸入,.placeholder textarea { padding: 4px;}}

Is there a freely available jQuery plugin that changes placeholder behavior to match HTML5 spec?

Before Focus

On Focus Good (Safari)

On Focus Bad (Chrome, Firefox)

You can what your browser does with this simple fiddle.

HTML5 draft spec says:

User agents should present this hint to the user, after having stripped line breaks from it, when the element's value is the empty string and/or the control is not focused (e.g. by displaying it inside a blank unfocused control and hiding it otherwise).

The "/or" is new in current draft so I suppose that's why Chrome and Firefox don't support it yet. See WebKit bug #73629, Chromium bug #103025.

解決方案

Stefano J. Attardi wrote a nice jQuery plugin that just does that.
It is more stable than Robert's and also fades to a lighter grey when the field gets focused.

  • See the demo page
  • Grab it on GitHub
  • Play with the fiddle

I modified his plugin to read placeholder attribute as opposed to manually creating a span.
This fiddle has complete code:

HTML

<input type="text" placeholder="Hello, world!">

JS

// Original code by Stefano J. Attardi, MIT license

(function($) {
    function toggleLabel() {
        var input = $(this);

        if (!input.parent().hasClass('placeholder')) {
            var label = $('<label>').addClass('placeholder');
            input.wrap(label);

            var span = $('<span>');
            span.text(input.attr('placeholder'))
            input.removeAttr('placeholder');
            span.insertBefore(input);
        }

        setTimeout(function() {
            var def = input.attr('title');
            if (!input.val() || (input.val() == def)) {
                input.prev('span').css('visibility', '');
                if (def) {
                    var dummy = $('<label></label>').text(def).css('visibility','hidden').appendTo('body');
                    input.prev('span').css('margin-left', dummy.width() + 3 + 'px');
                    dummy.remove();
                }
            } else {
                input.prev('span').css('visibility', 'hidden');
            }
        }, 0);
    };

    function resetField() {
        var def = $(this).attr('title');
        if (!$(this).val() || ($(this).val() == def)) {
            $(this).val(def);
            $(this).prev('span').css('visibility', '');
        }
    };

    var fields = $('input, textarea');

    fields.live('mouseup', toggleLabel); // needed for IE reset icon [X]
    fields.live('keydown', toggleLabel);
    fields.live('paste', toggleLabel);
    fields.live('focusin', function() {
        $(this).prev('span').css('color', '#ccc');
    });
    fields.live('focusout', function() {
        $(this).prev('span').css('color', '#999');
    });

    $(function() {
       $('input[placeholder], textarea[placeholder]').each(
           function() { toggleLabel.call(this); }
       );
    });

})(jQuery);

CSS

.placeholder {
  background: white;
  float: left;
  clear: both;
}
.placeholder span {
  position: absolute;
  padding: 5px;
  margin-left: 3px;
  color: #999;
}
.placeholder input, .placeholder textarea {
  position: relative;
  margin: 0;
  border-width: 1px;
  padding: 6px;
  background: transparent;
  font: inherit;
}

/* Hack to remove Safari's extra padding. Remove if you don't care about pixel-perfection. */
@media screen and (-webkit-min-device-pixel-ratio:0) {
    .placeholder input, .placeholder textarea { padding: 4px; }
}

這篇關于HTML5 占位符在焦點上消失的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
Updating table with Dynatable plugin(使用 Dynatable 插件更新表)
How to add new methods to the jQuery object?(如何向 jQuery 對象添加新方法?)
jQuery tooltip add line break(jQuery 工具提示添加換行符)
主站蜘蛛池模板: 亚洲www.| 国产精品一区二区不卡 | 色视频网站在线观看 | 成人性生交大免费 | 欧美日韩国产免费 | 久久亚洲一区 | 国产精品久久久久久久久久妞妞 | 久久国产精品久久久久久久久久 | 人人擦人人干 | 久久精品成人 | 黄色免费av | 久久综合久久久 | 欧美日韩中文字幕 | 亚洲毛片在线 | 国产一级在线 | 国产精品成av人在线视午夜片 | 日韩国产一区二区三区 | 国产精品国产三级国产aⅴ中文 | 成人一区在线观看 | 国产亚洲成av人片在线观看桃 | 在线观看www高清视频 | 中文字幕第九页 | 69堂永久69tangcom | 国产精品免费一区二区三区四区 | 久久中文字幕一区 | 在线播放中文 | 日日av| 亚洲经典一区 | 国产午夜精品一区二区三区四区 | 狠狠的干 | 欧美一级免费 | 国产精品一区二区三区久久 | 中文字幕在线观看国产 | 在线观看视频91 | 日韩www | 亚洲精品福利视频 | 亚洲欧美日韩在线 | 日本不卡在线观看 | 日本黄色大片免费 | 日韩成人免费 | 国产亚洲精品精品国产亚洲综合 |