如何删除焦点上的占位符
How to remove placeholder on focus
>我做了这个简单的函数,在不支持它的浏览器中添加占位符:
演示
问题是:当用户单击占位符时,我如何向该功能添加删除占位符的可能性?
尝试使用 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);
}
});
相关文章:
- 使用javascript的Asp.net内容占位符
- Internet Explorer缺少占位符支持,特别是密码字段
- 如何隐藏按钮单击事件上的占位符
- 需要URL模板占位符查找和替换功能的输入
- 初始化ng模型时,Angular ui选择占位符不起作用
- 如何在ie7或更高版本中设置密码字段的占位符
- 在不使用javascript的情况下,可以在表单字段中设置文本占位符(以提示最终用户插入特定格式)
- Select2-使用自定义模板时不显示占位符
- 替换字符串中的占位符值
- 为什么我应该使用HTML5(上下文:占位符)
- 在contentEditable元素中居中占位符插入符号
- 从气球弹出窗口中删除占位符
- 如何删除焦点上的占位符
- 占位符在焦点上保持可见
- 如何正确地控制或对表单字段焦点上的占位符执行某些操作
- 在IE10中,文本区占位符不会在焦点/输入时消失
- 如何使用jQuery删除焦点上的输入占位符文本
- 隐藏输入字段占位符(文本框水印)的焦点后一秒钟
- jQuery占位符/提示JavaScript保持提示的焦点
- 如何在焦点上显示占位符文本