これは、さまざまな JavaScript オプションによって完全に可能です。jquery の使用について a.stgeorge が投稿したことを補足するために、独自の JS を作成してそれを行うこともできます。
ここで動作を確認してください: http://www.moditekka.com/pwsample.html
HTML サンプル:
<input type="text" name="inpPass" id="inpPass" value="Password"/>
JavaScript スニペット (これは IE では失敗します。以下を参照してください):
var inpPass = document.getElementById("inpPass");
inpPass.onfocus = function() {
if(this.getAttribute("type") == "text") {
this.setAttribute("type", "password");
this.value = "";
}
}
inpPass.onblur = function() {
if(this.value == "") {
this.value = "Password";
this.setAttribute("type", "text");
}
}
編集: 誰かがまだこれを読んでいるなら、要素が DOM に追加された後にのみ "type" プロパティを読み取るというシアトルでの巧妙な決定のおかげで、私のきれいなコードが IE で機能しないことに気付きました。これに対応するために、以下の JavaScript コードを更新しました。
var inpPass = document.getElementById("inpPass");
inpPass.onfocus = function() {
if(this.getAttribute("type") == "text") {
var newInp = document.createElement('input');
newInp.onfocus = this.onfocus;
newInp.onblur = this.onblur;
newInp.ID = 'inpPass';
newInp.name = 'inpPass';
newInp.setAttribute('type', 'password');
this.parentNode.appendChild(newInp);
this.parentNode.removeChild(this);
newInp.focus();
}
}
inpPass.onblur = function() {
if(this.value == "") {
var newInp = document.createElement('input');
newInp.onfocus = this.onfocus;
newInp.onblur = this.onblur;
newInp.ID = 'inpPass';
newInp.name = 'inpPass';
newInp.setAttribute('type', 'text');
newInp.value = "Password";
this.parentNode.appendChild(newInp);
this.parentNode.removeChild(this);
}
}