zoukankan      html  css  js  c++  java
  • js修改input的type属性问题

    js修改input的type属性有些限制。当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题。但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改。在ff下仍是可读写属性。

    今天遇到个问题,输入框有默认值“密码”,但获得焦点时,“密码”两字会去掉,输入时直接变成”****“的password类型。很明显,一开始的时候,input的类型是text,后来变成了password类型。直观的思路是用js修改input的type类型。但ie下这么做不可行,所以只能换个思路,写两个input,一个text类型,一个password类型,分得监听onfocus和onblur事件。如下:

    ==================================================
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>阿当制作</title>
    </head>
    <style type="text/css">

    </style>
    <body>
    <input name="" type="text" value="密码" class="inputText_1" id="tx" style="100px;"  />
    <input name="" type="password" style="display:none;100px;" id="pwd" />
    <script type="text/javascript">
    var tx = document.getElementById("tx"), pwd = document.getElementById("pwd");
    tx.onfocus = function(){
    if(this.value != "密码") return;
    this.style.display = "none";
    pwd.style.display = "";
    pwd.value = "";
    pwd.focus();
    }
    pwd.onblur = function(){
    if(this.value != "") return;
    this.style.display = "none";
    tx.style.display = "";
    tx.value = "密码";
    }
    </script>
    </body>
    </html>
    =====================================
  • 相关阅读:
    Remove Element
    wso2esb安装及helloworld
    动态布局中RadioGroup的RadioButton有时候不相互排斥的原因
    有关机房收费系统学生下机的思考
    ITOO之底层关系
    POJ 3252 Round Numbers(数位dp&amp;记忆化搜索)
    怎样将「插件化」接入到项目之中?
    授人玫瑰 手留余香 --纪念python3.2.3官方文档翻译结束
    poj 2965 The Pilots Brothers&#39; refrigerator
    怎样使用本文档
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426960.html
Copyright © 2011-2022 走看看