How do I auto-hide placeholder text upon focus using css or jquery?

后端 未结 26 1747
长情又很酷
长情又很酷 2020-11-28 00:39

This is done automatically for every browser except Chrome.

I\'m guessing I have to specifically target Chrome.

Any solutio

相关标签:
26条回答
  • 2020-11-28 00:59

    HTML:

    <input type="text" name="name" placeholder="enter your text" id="myInput" />
    

    jQuery:

    $('#myInput').focus(function(){
      $(this).attr('placeholder','');
    });
    $('#myInput').focusout(function(){
      $(this).attr('placeholder','enter your text');
    });
    
    0 讨论(0)
  • 2020-11-28 01:01

    have you tried placeholder attr?

    <input id ="myID" type="text" placeholder="enter your text " />
    

    -EDIT-

    I see, try this then:

    $(function () {
    
        $('#myId').data('holder', $('#myId').attr('placeholder'));
    
        $('#myId').focusin(function () {
            $(this).attr('placeholder', '');
        });
        $('#myId').focusout(function () {
            $(this).attr('placeholder', $(this).data('holder'));
        });
    
    
    });
    

    Test: http://jsfiddle.net/mPLFf/4/

    -EDIT-

    Actually, since placeholder should be used to describe the value, not the name of the input. I suggest the following alternative

    html:

    <label class="overlabel"> 
        <span>First Name</span>
        <input name="first_name" type="text" />
    </label>
    

    javascript:

    $('.overlabel').each(function () {
        var $this = $(this);
        var field = $this.find('[type=text], [type=file], [type=email], [type=password], textarea');
        var span = $(this).find('> span');
        var onBlur = function () {
            if ($.trim(field.val()) == '') {
                field.val('');
                span.fadeIn(100);
            } else {
                span.fadeTo(100, 0);
            }
        };
        field.focus(function () {
            span.fadeOut(100);
        }).blur(onBlur);
        onBlur();
    });
    

    css:

    .overlabel {
      border: 0.1em solid;
      color: #aaa;
      position: relative;
      display: inline-block;
      vertical-align: middle;
      min-height: 2.2em;
    }
    .overlabel span {
      position: absolute;
      left: 0;
      top: 0;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .overlabel span, .overlabel input {
      text-align: left;
      font-size: 1em;
      line-height: 2em;
      padding: 0 0.5em;
      margin: 0;
      background: transparent;
      -webkit-appearance: none; /* prevent ios styling */
      border-width: 0;
      width: 100%;
      outline: 0;
    }
    

    Test:

    http://jsfiddle.net/kwynwrcf/

    0 讨论(0)
  • 2020-11-28 01:01

    With Pure CSS it worked for me. Make it transparent when Entered/Focues in input

     input:focus::-webkit-input-placeholder { /* Chrome/Opera/Safari */
        color: transparent !important;
     }
     input:focus::-moz-placeholder { /* Firefox 19+ */
       color: transparent !important;
     }
     input:focus:-ms-input-placeholder { /* IE 10+ */
       color: transparent !important;
     }
     input:focus:-moz-placeholder { /* Firefox 18- */
       color: transparent !important;
      }
    
    0 讨论(0)
  • 2020-11-28 01:01

    try this function:

    +It Hides The PlaceHolder On Focus And Returns It Back On Blur

    +This function depends on the placeholder selector, first it selects the elements with the placeholder attribute, triggers a function on focusing and another one on blurring.

    on focus : it adds an attribute "data-text" to the element which gets its value from the placeholder attribute then it removes the value of the placeholder attribute.

    on blur : it returns back the placeholder value and removes it from the data-text attribute

    <input type="text" placeholder="Username" />
    
    $('[placeholder]').focus(function() {
        $(this).attr('data-text', $(this).attr('placeholder'));
        $(this).attr('placeholder', '');
      }).blur(function() {
          $(this).attr('placeholder', $(this).attr('data-text'));
          $(this).attr('data-text', '');
      });
    });
    

    you can follow me very well if you look what's happening behind the scenes by inspecting the input element

    0 讨论(0)
  • 2020-11-28 01:02
    /* Webkit */
    [placeholder]:focus::-webkit-input-placeholder { opacity: 0; }
    /* Firefox < 19 */
    [placeholder]:focus:-moz-placeholder { opacity: 0; }
    /* Firefox > 19 */
    [placeholder]:focus::-moz-placeholder { opacity: 0; }
    /* Internet Explorer 10 */
    [placeholder]:focus:-ms-input-placeholder { opacity: 0; }
    
    0 讨论(0)
  • 2020-11-28 01:04
    <input 
    type="text" 
    placeholder="enter your text" 
    onfocus="this.placeholder = ''"
    onblur="this.placeholder = 'enter your text'" />
    
    0 讨论(0)
提交回复
热议问题