2015-04-03 116 views
1

所以我有一个密码字段,其值设置为“密码”。我有一些非常简单的JavaScript,它将值设置为“onfocus”。像往常一样,我想要将密码屏蔽掉,并用点代替。问题是,如果我将该字段设为密码字段,则原始值“密码”将显示为点。有没有简单的方法来使原始值“密码”,其余的,在onfocus之后,点?或者,我必须在onfocus之后更改字体以及只有点的自定义字体吗?密码字段的问题

这里是我的HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <title>SuM BUTtonsS DOe</title> 
    <link rel="stylesheet" href="buttons.css"/> 
</head> 
<body> 
    <p>Please enter the password.</p> 
    <form> 
     <input id="password" type="textbox" value="Password" onfocus="if (this.value=='Password') this.value='';" onblur="if (this.value=='') this.value='Password';"/> 
    </form> 
</body> 
</html> 

回答

0

你并不需要的JavaScript。使用placeholder属性。最糟糕的情况是,它不被支持,并且该字段只是空的。 (虽然是widely supported now)。

<input id="password" type="password" placeholder="Password">

+0

只是想知道,有没有什么办法改变占位符文本的颜色/透明度? – Tommay 2015-04-03 15:59:59

+0

http://stackoverflow.com/questions/2610497/change-an-inputs-html5-placeholder-color-with-css – Touffy 2015-04-03 16:06:07

0

它会更简单,只需使用类型= '密码':

<input id="password" type="password" value="Password" /> 
0

最好的办法是使用占位符Toofy他回答说。

如果你想这样做在你启动的方式,改变类型属性文本和密码之间切换:

<input id="password" type="textbox" value="Password" onfocus="if (this.value=='Password') { this.value='';this.setAttribute('type','password')};" onblur="if (this.value=='') { this.value='Password';this.setAttribute('type','text'); }"/> 

小提琴这里:http://jsfiddle.net/1y7Levr4/