2009-12-15 68 views
0

我已經問了一個問題今天:This one跟進有關輸入焦點問題和模糊

現在我有工作的代碼,但只有在FF(完全)和部分在IE的作品(7,8)。

function replaceT(obj){ 
    var newO=document.createElement('input'); 
    newO.className = obj.className; 
    newO.style.width = '118px'; 
    newO.style.height = '17px'; 
    newO.style.color = '#666'; 
    newO.setAttribute('maxlength','30'); 
    newO.setAttribute('type','password'); 
    newO.setAttribute('onblur','this.value=\'Password\'; this.type=\'text\''); 
    newO.setAttribute('onfocus','this.value=\'\'; this.type=\'password\''); 
    newO.setAttribute('tabindex','2'); 
    newO.setAttribute('value',''); 
    newO.setAttribute('id','password_property_input'); 
    newO.setAttribute('name',obj.getAttribute('name')); 
    obj.parentNode.replaceChild(newO,obj); 
    newO.focus(); 
} 

在Firefox中一切都運行完美,我呼籲的焦點此功能,所以我的舊型密碼輸入變爲鍵入文本輸入。

現在,當我點擊此輸入(onblur)之外時,我再次將字段類型更改爲文本,並將值恢復爲密碼。如果再次單擊該字段(onfocus),我的文本類型輸入再次變爲密碼,並且該值返回到「'。

這在FF中完美的工作,但沒有一個在IE中工作,當我說我的意思是沒有onblur和onfocus。從文本到密碼輸入的第一次更改有效。感謝您的任何答案

我找到了更好的解決方案,但它也並不在IE瀏覽器:

內window.load功能的說:

applyPasswordType(document.getElementById('password_property_input'), 'Password', 'text'); 

和函數本身。 。

function applyPasswordType(elem, val, typ) { 
     elem.value = val; 
     elem.type = typ; 
     elem.onfocus = function() { 
     if(this.value == val) { 
      this.style.color = ''; 
      this.type = 'password'; //If in focus, input type will be 'password' 
      this.value = ''; 
     } 
     } 

     elem.onblur = function() { 
     if(this.value == '') { 
      this.value = val; 
      this.type = 'text'; //On blur, input type will be 'text' in order to show value 
     } 
     } 
    } 

我仍然試圖找出如何使用的addEventListener解決這個問題..

回答

1

setAttribute在IE中壞了,請不要使用它。

直接使用性質:

foo.onevent = function() { ... } 

更重要的是,使用addEventListener/attachEvent

您可能還會發現,IE瀏覽器不能更改現有輸入的類型。如果是這樣,你將不得不創建一個新的輸入並替換現有的輸入。

+0

我應該使用什麼? – ant 2009-12-15 14:27:02

+0

所以我應該每次在IE中更換輸入?有沒有辦法繞過它?我的意思是採取另一種方法來展示我想要做的事情的全貌。我想讓我的密碼具有「密碼」的值,並且在使用密碼掩碼更改爲實際密碼字段時,還要使用onblur來恢復密碼字段的密碼值。 – ant 2009-12-15 14:34:46

+0

也可以澄清「foo.onevent =函數(){...} 」部分我不知道你在說什麼(我的錯)。謝謝 – ant 2009-12-15 14:35:39