我想要我的 <input>
元素具有占位符文本,当输入具有焦点(并且为空)时该文本仍然可见。但是,我不能使用实际的 placeholder
属性,因为它只在输入没有焦点时出现在 IE10 和 Safari 中,并且在旧的 IE 版本中不受支持。什么是跨浏览器解决方案,最好不要使用 jquery(因为我没有其他理由使用它)?
最佳答案
如果你可以在这里使用 jQuery 是一个选项
演示 http://jsfiddle.net/kevinPHPkevin/FdjJv/8/
$(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);
}
});
});
要获得完整的浏览器支持,您可以使用 http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html 等库
已编辑
我会推荐一个 jQuery 插件,然后你可以将它分配给你的输入
演示 http://jsfiddle.net/kevinPHPkevin/5j55m/136/
<input type='text' placeholder='this is a test' />
<input type='password' placeholder='testing password' />
关于javascript - 占位符在焦点上仍然可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422955/