Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript输入焦点_Javascript_Css_Placeholder - Fatal编程技术网

javascript输入焦点

javascript输入焦点,javascript,css,placeholder,Javascript,Css,Placeholder,我将默认值“visitor”设置为输入框。当焦点在文本框中或MOOUSE输入时,我想隐藏“visitor”字符串,并在它失去焦点或mose移出时显示它。尝试使用HTML5占位符属性: <form id="commentform" method="post" action="wp-comments-post.php"> <input type="text" aria-required="true" tabindex="1" size="22" value=""

我将默认值“visitor”设置为输入框。当焦点在文本框中或MOOUSE输入时,我想隐藏“visitor”字符串,并在它失去焦点或mose移出时显示它。

尝试使用HTML5占位符属性:

<form id="commentform" method="post" action="wp-comments-post.php">
    <input type="text" aria-required="true" tabindex="1" size="22" value="" 
       id="author" name="author">
</form>

虽然浏览器支持还不是100%,但这将为您提供一种标准的方式来实现您想要实现的目标,而不必经历不必要的麻烦

您可以尝试的另一件事是将输入元素覆盖在一些文本上,并使其在不聚焦时透明/半透明,在聚焦/填充时不透明

今天的登录页面使用了以下技巧:

<input type="text" aria-required="true" tabindex="1" size="22"
    placeholder="visitor" id="author" name="author">

密码
通过CSS魔法,这将成为:


看起来您正在使用WordPress,因此您的站点上有jQuery库

你可以用我的方法来实现这一点

例子

jQuery 在这种情况下,我必须自己指定标签,但是插件通过查找
输入
的相关
标签
元素来运行,该元素应该是可访问的


.

如果您已达到HTML 5,请尝试以下操作:

$('#author').inputLabel({
   customLabel: 'Visitor'
});

var prompt=“访问者”;
var txt=document.getElementById(“作者”);
txt.onfocus=function(){txt.value='';}
txt.onblur=函数(){
如果(txt.value=''){
txt.value=prompt;
}
}

Ates Goral的答案看起来很有趣。请先试一试。如果您不想流汗,这是另一种选择:)
我建议使用水印插件。有很多可用的。
我以前用过。很好。给你很好的控制。

该插件需要jQuery,尽管我也会使用jQuery或CSS以及伪类(:focus)

这里有一个简单的JS解决方案,它完全符合您的要求。同样,我不建议对一个或两个以上的输入字段使用这种方法


然而,此方法的问题在于它不能很好地进行验证。@alex毫无疑问,验证将需要额外的工作,如果用户输入相同的字符串(“访问者”)。如果您已经有了jquery(在本例中是,因为wordpress),则扩展非常好。“添加到我的工具集中。”村民们非常感谢您的友好话语。请随意谈论任何问题。
$('#author').inputLabel({
   customLabel: 'Visitor'
});
<script type="text/javascript">
var prompt="visitor";       
var txt=document.getElementById("author");
txt.onfocus=function(){txt.value='';}
txt.onblur=function(){
            if(txt.value==''){
                txt.value=prompt;
            }
        }
</script>