Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改<;输入>;使用JavaScript键入IE_Javascript_Html_Internet Explorer - Fatal编程技术网

更改<;输入>;使用JavaScript键入IE

更改<;输入>;使用JavaScript键入IE,javascript,html,internet-explorer,Javascript,Html,Internet Explorer,以下代码适用于除IE以外的所有web浏览器: <input type="text" name="passwordLogin" value="Password" onfocus="if(this.value=='Password'){this.value=''; this.type='password'};" onblur="if(this.value==''){this.value='Password'; this.type='text'};" size="25" /> 不能在In

以下代码适用于除IE以外的所有web浏览器:

<input type="text" name="passwordLogin" value="Password" onfocus="if(this.value=='Password'){this.value=''; this.type='password'};" onblur="if(this.value==''){this.value='Password'; this.type='text'};" size="25" />

不能在Internet Explorer中动态更改输入元素的类型

一个可能的解决办法是:

  • 动态创建新元素
  • 将旧元素的属性复制到新元素中
  • 将新元素的类型设置为新类型
  • 然后用新元素替换旧元素
您可能希望查看以下文章,了解上述内容的JavaScript植入:

另一种选择是静态创建两个元素,但一次只能看到一个。可见性和焦点取决于元素的值。在这种情况下,您可能需要使用以下内容:

<script type="text/javascript">   
  function checkType() {
     var thisElement = document.getElementById('field_text');
     var otherElement = document.getElementById('field_password');

     if (thisElement.value === 'Password') {            
        otherElement.style.display = 'inline';
        thisElement.style.display = 'none';
        otherElement.focus();
     }
  }
</script>

<input type="text" value="Password" id="field_text" onfocus="checkType();" />
<input type="password" value="" style="display: none;" id="field_password" />

函数checkType(){
var thiseelement=document.getElementById('field_text');
var otherElement=document.getElementById('field_password');
如果(thisElement.value=='Password'){
otherElement.style.display='inline';
thisElement.style.display='none';
otherElement.focus();
}
}

第三个选项是更改类名而不是值,然后只更改焦点上的bg图像,而不是类型。

您可以这样做。但是,在outerhtml上执行替换实际上是重新声明元素,因此任何未在标记声明中显式定义的属性都将被忽略。这就是文本值首先保存到变量的原因。与jQuery的attr和prop不同,这在IE的所有版本中都有效。我使用了真正的IE10,在5.5到9版本中使用了IETester

,代码如下:

HTML

<input id="myinput" type="password">
<input value="transform" id="transformButton" type="button">

您可以使用下面的代码在IE中使用JavaScript更改
类型:

<html>

<head>
<meta http-equiv="X-UA-Compatible" content="IE=5" />

<script>

//this function will toggle the input between being a password or a text input
function transform(srcc,status) {


    //copy the element itself, its html source, and value text to a variable
    var myInput = document.getElementById(srcc.id);
    var oldHtml = myInput.outerHTML;
    var text = myInput.value;
    if (status=="click")
    {
        //replace "password" with "text" in the html if it is a password field
        var newHtml = oldHtml.replace(/password/g, "text");

    //update the html
    myInput.outerHTML = newHtml;
    //restore the text value
    myInput = document.getElementById(srcc.id);
    myInput.value = text;   
    document.getElementById(srcc.id).focus();
    }
    else
    {
        //replace "text" with "password" if it is a text field      
var newHtml = '<input id='+srcc.id+' type="password" value="cc" onclick="transform(this,\'click\');"  onblur="transform(this,\'blur\');" onmouseout="transform(this,\'mouseout\');">';


    //update the html
    myInput.outerHTML = newHtml;
    //restore the text value
    myInput = document.getElementById(srcc.id);
    myInput.value = text;
    }

}
</script>
</head>

<body>
 <input id="myinput" type="password" value="cc" onclick="transform(this,'click');"  onblur="transform(this,'blur');" onmouseout="transform(this,'mouseout');">
</body>

</html>

//此功能将在密码或文本输入之间切换输入
函数转换(srcc,状态){
//将元素本身、其html源和值文本复制到变量
var myInput=document.getElementById(srcc.id);
var oldHtml=myInput.outerHTML;
var text=myInput.value;
如果(状态=“单击”)
{
//如果是密码字段,则将html中的“密码”替换为“文本”
var newHtml=oldHtml.replace(/password/g,“text”);
//更新html
myInput.outerHTML=newHtml;
//恢复文本值
myInput=document.getElementById(srcc.id);
myInput.value=文本;
getElementById(srcc.id).focus();
}
其他的
{
//如果是文本字段,则将“文本”替换为“密码”
var newHtml='';
//更新html
myInput.outerHTML=newHtml;
//恢复文本值
myInput=document.getElementById(srcc.id);
myInput.value=文本;
}
}

dang这就是我以前的经历,我被告知这是错误的=[grr=[适用于IE9及以上版本。IE9及以上版本将允许您直接更改类型属性。这在IE8中也适用。为什么这个答案获得如此少的向上投票数?惊人的答案,伟大的心智计划,兄弟。我同意@JrChen,这个答案应该有几百个向上的拇指。我添加了这个,所以您可以通过“显示/隐藏密码”复选框进行切换:如果(oldHtml.indexOf(“\”password\”)>-1){newHtml=oldHtml.replace(“\”password\”,“\”text\”);}其他{newHtml=oldHtml.replace(“\”text\”,“\”password\”);}谢谢,chiliNUT。你能解释一下这个代码段的作用吗?
<script type="text/javascript">   
  function checkType() {
     var thisElement = document.getElementById('field_text');
     var otherElement = document.getElementById('field_password');

     if (thisElement.value === 'Password') {            
        otherElement.style.display = 'inline';
        thisElement.style.display = 'none';
        otherElement.focus();
     }
  }
</script>

<input type="text" value="Password" id="field_text" onfocus="checkType();" />
<input type="password" value="" style="display: none;" id="field_password" />
<input id="myinput" type="password">
<input value="transform" id="transformButton" type="button">
//attach a click handler to the button to make it transform 
//when clicked, via our transform() function below
document.getElementById('transformButton').addEventListener("click", transform);

//flag of whether or not it is a password field or text field
var isPassword = true;
//this function will toggle the input between being a password or a text input
function transform() {
    //copy the element itself, its html source, and value text to a variable
    var myInput = document.getElementById("myinput");
    var oldHtml = myInput.outerHTML;
    var text = myInput.value;
    if (isPassword)
    {
        //replace "password" with "text" in the html if it is a password field
        var newHtml = oldHtml.replace(/password/g, "text");
    }
    else
    {
        //replace "text" with "password" if it is a text field
        newHtml = oldHtml.replace(/text/g, "password");
    }
    //update the html
    myInput.outerHTML = newHtml;
    //restore the text value
    myInput = document.getElementById("myinput");
    myInput.value = text;
    //toggle the isPassword flag
    isPassword = !isPassword;
}
<html>

<head>
<meta http-equiv="X-UA-Compatible" content="IE=5" />

<script>

//this function will toggle the input between being a password or a text input
function transform(srcc,status) {


    //copy the element itself, its html source, and value text to a variable
    var myInput = document.getElementById(srcc.id);
    var oldHtml = myInput.outerHTML;
    var text = myInput.value;
    if (status=="click")
    {
        //replace "password" with "text" in the html if it is a password field
        var newHtml = oldHtml.replace(/password/g, "text");

    //update the html
    myInput.outerHTML = newHtml;
    //restore the text value
    myInput = document.getElementById(srcc.id);
    myInput.value = text;   
    document.getElementById(srcc.id).focus();
    }
    else
    {
        //replace "text" with "password" if it is a text field      
var newHtml = '<input id='+srcc.id+' type="password" value="cc" onclick="transform(this,\'click\');"  onblur="transform(this,\'blur\');" onmouseout="transform(this,\'mouseout\');">';


    //update the html
    myInput.outerHTML = newHtml;
    //restore the text value
    myInput = document.getElementById(srcc.id);
    myInput.value = text;
    }

}
</script>
</head>

<body>
 <input id="myinput" type="password" value="cc" onclick="transform(this,'click');"  onblur="transform(this,'blur');" onmouseout="transform(this,'mouseout');">
</body>

</html>