[英]How to avoid showing hint text in textbox using javascript
I am working on simple login form. 我正在使用简单的登录表单。 I have two text fields as user name and password.
我有两个文本字段,分别是用户名和密码。
user name text field have hint as "user name". 用户名文本字段的提示为“用户名”。
password text field have hint as "password". 密码文本字段的提示为“密码”。
I want to hide hint text while clicking inside textbox. 我想在文本框内单击时隐藏提示文本。
here i faced two problems, 在这里我遇到两个问题,
if i click inside user name text box the hint text is hiding. 如果我在用户名文本框中单击,则提示文本将隐藏。 but when i press ctrl+z then again the hint appearing and able to edit by pressing keyboard arrow keys.
但是当我按ctrl + z时,提示再次出现,并且可以通过按键盘上的箭头键进行编辑。
second one is the password text field type is password. 第二个是密码文本字段类型是密码。 so it is not showing hint text as "password".
因此它不会将提示文本显示为“密码”。
here is my code: 这是我的代码:
<html>
<head>
<title>login</title>
<script>
function ForUserName()
{
if(document.getElementById('username').value == "user name")
{
document.getElementById('username').value='';
}
else if(document.getElementById('username').value == '')
{
document.getElementById('username').value='user name';
}
}
function ForPassword()
{
if(document.getElementById('password').value == "password")
{
document.getElementById('password').value='';
}
else if(document.getElementById('password').value == '')
{
document.getElementById('password').value='password';
}
}
</script>
</head>
<body>
<input type="text" id="username" value="user name" onclick="ForUserName();" onblur="ForUserName();">
<input type="password" id="password" value="password" onclick="ForPassword();" onblur="ForPassword();">
</body>
</html>
can any one please... 可以请任何一个...
You could use HTML5′s placeholder Attribute 您可以使用HTML5的占位符属性
<input type="text" id="username" placeholder="user name">
<input type="password" id="password" placeholder="password">
For supporting old browsers: 对于支持旧版浏览器:
<input type="text" id="username" value="user name" onfocus="if (this.value == 'user name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'user name';}">
<input type="password" id="password" value="password" onfocus="if (this.value == 'password') {this.value = '';}" onblur="if (this.value == '') {this.value = 'password';}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.