2009-12-15 40 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