gpt4 book ai didi

javascript - 占位符在焦点上仍然可见

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:06 25 4
gpt4 key购买 nike

我想要我的 <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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com