HTML5 placeholder disappears on focus

前端 未结 6 865
广开言路
广开言路 2020-11-29 01:58

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

Before Focus

6条回答
  •  清酒与你
    2020-11-29 02:20

    Stefano labels

    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

    
    

    JS

    // Original code by Stefano J. Attardi, MIT license
    
    (function($) {
        function toggleLabel() {
            var input = $(this);
    
            if (!input.parent().hasClass('placeholder')) {
                var label = $('

    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; }
    }
    

提交回复
热议问题