如何删除焦点上的占位符

How to remove placeholder on focus

本文关键字:焦点 占位符 删除 何删除      更新时间:2023-09-26

>我做了这个简单的函数,在不支持它的浏览器中添加占位符:

演示

问题是:当用户单击占位符时,我如何向该功能添加删除占位符的可能性?

尝试使用 removeAttr() 像,

$('input,textarea').focus(function(){
   $(this).removeAttr('placeholder');
});

演示

要再次获得placeholder value blur()请尝试此操作,

$('input,textarea').focus(function(){
   $(this).data('placeholder',$(this).attr('placeholder'))
          .attr('placeholder','');
}).blur(function(){
   $(this).attr('placeholder',$(this).data('placeholder'));
});

演示 1

不需要使用 javascript 函数来完成此操作,更简单的解决方案是:

<input type="text" placeholder="enter your text" onfocus="this.placeholder=''" onblur="this.placeholder='enter your text'" />

CSS 对我有用:

input:focus::-webkit-input-placeholder {
    color: transparent;
}
$("input[placeholder]").each(function () {
    $(this).attr("data-placeholder", this.placeholder);
    $(this).bind("focus", function () {
        this.placeholder = '';
    });
    $(this).bind("blur", function () {
        this.placeholder = $(this).attr("data-placeholder");
    });
});

一个非常简单和全面的解决方案适用于Mozila,IE,Chrome,Opera和Safari:

<input type="text" placeholder="your placeholder" onfocus="this.placeholder=''" onblur="this.placeholder='your placeholder'" />
 $('*').focus(function(){
  $(this).attr("placeholder",'');
  });

试试这个希望它有帮助

$('input,textarea').focus(function()
{
$(this).attr('placeholder','');
});
$('input').focus(function()
{
  $(this).attr('placeholder','');
});

对于不支持占位符的浏览器,您可以使用以下内容:https://github.com/mathiasbynens/jquery-placeholder。像HTML5一样正常添加占位符属性,然后调用此插件:$('[placeholder]').placeholder();。然后使用Rohan Kumar的代码,将是跨浏览器的。

这是我

在点击而不是焦点上的解决方案:

$(document).on('click','input',function(){
    var $this = $(this);
    var place_val = $this.attr('placeholder');
    if(place_val != ''){
        $this.data('placeholder',place_val).removeAttr('placeholder');
    }
}).on('blur','input',function(){
    var $this = $(this);
    var place_val = $this.data('placeholder');
    if(place_val != ''){
        $this.attr('placeholder',place_val);
    }
});