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

后端 未结 26 1854
长情又很酷
长情又很酷 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 01:04

    To further refine Wallace Sidhrée's code sample:

    $(function()
    {  
          $('input').focusin(function()
          {
            input = $(this);
            input.data('place-holder-text', input.attr('placeholder'))
            input.attr('placeholder', '');
          });
    
          $('input').focusout(function()
          {
              input = $(this);
              input.attr('placeholder', input.data('place-holder-text'));
          });
    })
    

    This ensures that each input stores the correct placeholder text in the data attribute.

    See a working example here in jsFiddle.

提交回复
热议问题