2015-04-03 61 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/