Javascript 如何删除焦点上的占位符

Javascript 如何删除焦点上的占位符,javascript,jquery,focus,placeholder,Javascript,Jquery,Focus,Placeholder,我使用此简单函数在不支持占位符的浏览器中添加占位符: 问题是:当用户在占位符中单击时,如何向该函数添加删除占位符的可能性?尝试使用like 要在blur()上再次获取占位符值,请尝试以下操作: $('input,textarea').focus(function(){ $(this).data('placeholder',$(this).attr('placeholder')) .attr('placeholder',''); }).blur(function(){

我使用此简单函数在不支持占位符的浏览器中添加占位符:

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

尝试使用like

要在
blur()
上再次获取
占位符值,请尝试以下操作:

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

试试这个,希望对你有帮助

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

});

对于不支持占位符的浏览器,可以使用以下选项: . 与HTML5一样,添加占位符属性,然后调用此插件:
$(“[placeholder]”)。placeholder()。然后使用Rohan Kumar的代码,并将跨浏览器访问

CSS为我工作:

input:focus::-webkit-input-placeholder {
    color: transparent;
}

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

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

一个非常简单而全面的解决方案是在Mozila、IE、Chrome、Opera和Safari中使用:

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

这是我在单击非焦点时的解决方案:

$(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);
    }
});

为什么你要使用占位符你可以使用模糊占位符是浏览器ie的内置属性不支持此记忆!谢谢你,罗汉。它在铬合金中工作正常。Altough不在Explorer(第8节)中工作,这正是我最需要这个函数的地方。您知道如何使这段代码也在Explorer 8上工作吗?非常感谢。如果你需要我创建一个新的JSBin:@johnnyfittizio,这是因为
占位符
IE 8
中不起作用,为此你必须在
上设置
元素的
,然后对其进行聚焦和模糊
参见演示如何在你发布的代码中准确地做到这一点?谢谢谢谢你,罗汉!不知怎的,我错过了去年的巡回赛。。它工作得很好!现代浏览器的最佳解决方案。FireFoxNilesh的任何解决方案:很好!它正在工作perfectly@C_J这太棒了。。
<input type="text" placeholder="enter your text" onfocus="this.placeholder=''" onblur="this.placeholder='enter your text'" />
$("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");
    });
});
<input type="text" placeholder="your placeholder" onfocus="this.placeholder=''" onblur="this.placeholder='your placeholder'" />
$(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);
    }
});