【问题标题】:Placeholder delete on focus占位符删除焦点
【发布时间】:2012-12-29 15:51:47
【问题描述】:

我正在使用“占位符”属性。用于输入字段中的占位符文本

我也需要在 IE 化身中具有此功能。

我希望占位符隐藏在 FOCUS 上!当输入第一种类型时,我发现的每个解决方法都会隐藏占位符。

我自己无法解决,它需要简单地删除输入的占位符文本,但如果输入值为空,还需要恢复模糊时的占位符文本。

我正在努力解决这个问题

jQuery(document).ready(function () {

jQuery('[placeholder]').focus(function() {
  input = jQuery(this);
  if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
    input.attr('placeholder','')
  }
}).blur(function() {
  var input = jQuery(this);
  if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
  }
}).blur().parents('form').submit(function() {
  jQuery(this).find('[placeholder]').each(function() {
    var input = jQuery(this);
    if (input.val() == input.attr('placeholder')) {
      input.val('');
    }
  })
});


});

这会将占位符的值更改为“(无)”,并且行为如我所愿,但在模糊时它无法恢复占位符值,因为它的“(无)”现在

//更新

我的解决方案:

$(function()
{
  $('input').each(function() 
  {
     $(this).val($(this).attr('holder'));
  });

  $('input').focus(function()
  {
    if($(this).attr('holder')==$(this).val())
    {
      $(this).val('');
    }
  });
  $('input').focusout(function()
  {
     if($.trim($(this).val())=='')
     {
       var holder = $(this).attr('holder');
       $(this).val(holder);
     }
  });
    });

【问题讨论】:

    标签: javascript jquery html attributes placeholder


    【解决方案1】:

    如果你不想使用placeholder,你可以使用下面的代码或者使用这个简单的jQuery插件:

    jQuery Placeholder

    jsFiddle Live Demo Of Code Below

    HTML

    <input type='text' holder='First name'><br>
    <input type='text' holder='Last name'><br>
    <input type='text' holder='Age'>
    


    JavaScript

    $(function() {
    
      $('input').each(function() {
         $(this).val($(this).attr('holder'));
      });
    
      $('input').focus(function() {
        if($(this).attr('holder')==$(this).val()) {
          $(this).val('');
        }
      });
    
      $('input').focusout(function() {
         if($.trim($(this).val())=='') {
           var holder = $(this).attr('holder');
           $(this).val(holder);
         }
      });
    });
    

    您也可以使用这个简单的 jQuery 插件:jQuery Placeholder

    【讨论】:

    • IE lte 9 和 Opera 最初没有 attr('holder') 的值,通过添加 $('#selector').val($('#selector').attr ('持有者'));功能之外
    • 很好,每个循环都比我的尝试更具原型性
    【解决方案2】:

    使用占位符属性:

    <input type="text" placeholder="Search here">
    

    对于不支持placeholder的浏览器;你可以使用polyfill

    【讨论】:

    • 我正在这样做,也需要 ie-incarnations 中的 tat 功能,看看我编辑的问题
    • 必须有一个更简单的解决方案,看看我的编辑,感谢您的时间
    • 没有比添加placeholder="whatever"更简单的了;对于 IE,简单地通过 yepnope 添加一个 polyfill 不会给您带来任何问题。请记住,这些 polyfill 已经过彻底测试,并且可能比“更简单”的解决方案表现得更好。让它消失onkeydown 是标准行为,不要通过改变它来疏远你的用户:)
    • 我不希望占位符在焦点上存在,placeholder="" 的功能让占位符在我输入字段而不是焦点时隐藏
    【解决方案3】:

    对于基于 Webkit 的浏览器和 Firefox,您可以在纯 CSS 中隐藏占位符:

    input:focus::-webkit-input-placeholder, 
    input:focus::-moz-placeholder { opacity:0; }
    

    【讨论】:

      【解决方案4】:

      旧而不是最好的方法是:

      <input type="text" name="fname" value="Enter text here" onFocus="if (this.value=='Enter text here'){this.value='';}"
      onBlur="if (this.value==''){this.value='Enter text here';}" />
      

      但它甚至在 IE6 中也有效。

      【讨论】:

      • 是的,这正是我正在寻找的
      • 记得在提交时清除占位符值,否则服务器将收到虚拟值。
      【解决方案5】:

      有许多 polyfill 可以在不支持它的浏览器中“添加”占位符功能。我过去使用过jQuery Placeholder,但modernizr docs 中提到了很多其他的(向下滚动到占位符部分)。

      jQuery Placeholder 的好处在于,除了对库的引用之外,您不必更改/添加任何代码。只需将placeholder 属性添加到您的元素即可。

      【讨论】:

      【解决方案6】:

      这是我在输入和 textarea focus() 上隐藏占位符文本的代码。简单而且效果很好。

      $('textarea,input').focus(function(e){
          val=$(this).attr('value');
          if (!val)
          $(this).attr('value',' ');
          this.select();
      }).blur(function(e){
          val=$(this).attr('value');
          if (val==' ')
          $(this).attr('value','');
      });
      

      【讨论】:

        【解决方案7】:

        我使用这两种方法(jquery)在输入获得焦点时隐藏占位符,当输入失去焦点并且为空时显示占位符。

        输入.焦点

        • 第 1 步:简单地将占位符值保存到通用变量中(此处为 pl)
        • 第 2 步:将输入的占位符属性设置为 ''

        input.focusout

        • 步骤一:将输入的占位符属性设置为保存的变量(此处为pl)

           var pl;
          //on getting focus
          $('input').focus(function(){
            pl=$(this).prop('placeholder');
            $(this).prop('placeholder','');
          });
          // on losing focus:
          $('input').focusout(function(){    
            $(this).prop('placeholder',pl);
          });
          

        【讨论】:

        • 只有答案中的代码是不够的。需要一些解释。
        猜你喜欢
        • 1970-01-01
        • 2013-11-09
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 2023-03-10
        • 1970-01-01
        相关资源
        最近更新 更多