简体   繁体   English

在IE8中使用replaceChild更改输入类型不起作用

[英]Changing type of input is not working in IE8 with replaceChild

I am using below code to change input type onfocus and resetting it to text onblur. 我正在使用下面的代码更改输入类型onfocus并将其重置为文本onblur。 But this is not working 但这不起作用

HTML HTML

 <input type="password" onfocus="changeInputType(this,'text')"
                        onblur="changeInputType(this,'password')">

JS JS

  function changeInputType(oldObject, oType) {
     var newObject = document.createElement('input');
     var wrapper= document.createElement('div');
     wrapper.innerHTML= oldObject.outerHTML.replace('type=password','type=text');
     var newObject = wrapper.firstChild;
     oldObject.parentNode.replaceChild(newObject,oldObject);
     return newObject;
  }

here is the fiddle. 是小提琴。 It is not working. 它不起作用。

Assuming that you want to change the type attribute to password on blur and to text on focus, your code does not work for more than one reason. 假设您要将模式属性更改为模糊上的密码和焦点上的文本,则代码不会出于多种原因而起作用。

  1. You are missing the double quotes in your call to replace: replace('type=password','type=text') 您在调用替换时缺少双引号: replace('type=password','type=text')

  2. When you call replaceChild the tag is removed and another blur event is triggered causing an error. 当您调用replaceChild ,标记将被删除,并触发另一个模糊事件,从而导致错误。

  3. You are not using the oType variable anywhere. 您没有在任何地方使用oType变量。

Is easier to just use the type property like in the code below: 更容易使用类型属性,如下面的代码:

function changeInputType(oldObject, oType) {
    oldObject.type = oType;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM