2011-06-26 60 views
0

我正在開發我的網站上的登錄部分,並希望它使用相同的效果,例如,在Textfield和Password字段爲「是」時刪除了用戶名和密碼值關注焦點。我一直在使用普通的JavaScript這與以下幾行的一些嘗試......登錄文本字段和密碼字段刪除焦點上的文本

function PasswordClick(evt) { 

    if ("Password" === this.value) this.value = ""; 
if(this.getAttribute('type')=='text') 
     { 
       this.setAttribute('type','password'); 
     } 
     this.focus(); 
} 

這似乎導致使用Chrome 12.0.742.100在Windows上的問題和概率等網絡套件瀏覽器,但在Firefox正常工作。

有沒有人有更好的方式做到這一點?

在此先感謝。

回答

0

此代碼應該做你想要什麼。這部分應該在你的頁面的HEAD部分:

function init() { 
    document.getElementById('password').type = 'text'; 
} 
window.onload = init; 

function gotFocus(item) { 
    if (item.value == 'Password') { 
     item.value = ''; 
     item.type = 'password'; 
     item.style.color = ''; 
    } 
} 

function lostFocus(item) { 
    if(item.value == '') { 
     item.value = 'Password'; 
     item.type = 'text'; 
     item.style.color = '#C0C0C0'; 
    } 
} 

然後您要輸入的密碼是,你可以把這個HTML:但是

<input id='password' name='password' type='password' onfocus="gotFocus(this);" onblur="lostFocus(this);" value="Password" style="color:#C0C0C0" />