我想实现一个显示隐藏密码的功能
代码如下
在IE9 FIREFOX上都正常,但是IE6 7 8无效
请问应该如何实现呢    <script  type="text/javascript">
        function showPwd() {
            var oCB = document.getElementById("CBShowPwd");
            var oTB = document.getElementById("tbPwd");
            if (oCB.checked == true) {
                oTB.type = "text";
            }
            else {
                oTB.type = "password";
            }
        }
    </script>IE6JavaScriptjstype兼容

解决方案 »

  1.   

    你的这个代码在ie9上能实现?我的ie9怎么没反应?
      

  2.   

    http://hi.baidu.com/cly84920/item/f97034d75ffd8fcb1a72b45b
      

  3.   

    可以的,我IE9 FIREFOX都试过了
      

  4.   

    你在复选框加一个onchange就可以了
      

  5.   

    ie中<input>的type属性是只读的。不能修改
    可以新创建个<input>把原来的替换掉
      

  6.   


    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <style type="text/css">
    body{font:12px "宋体";}
    #aa{cursor:pointer;}
    </style>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $("#aa").click(function(){
    $("#bb").attr("type","password");
    });
    });
    </script>
    </head>
    <body>
    <div id="aa">改变密码</div>
    <input type="text" id="bb">
    </body>
    </html>
    为什么不能用呢????
      

  7.   

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>test</title>
    </head>
    <body>
        密码:<input type="password" id="password" />
        <input type="button" id="show_password" value="显示密码"/>
        <script type="text/javascript">
            window.onload = function(){
                var showButton = document.getElementById('show_password');
                var x = 1;
                showButton.onclick = function(){
                    var password = document.getElementById('password');
                    try{
                        password.type = 'text'==password.type ? 'password' : 'text';
                    }catch(e){
                        var clone = document.createElement('input');
                        clone.type = ['text','password'][x=x^1];
                        clone.value = password.value;
                        clone.id = 'password';
                        var parent = password.parentNode;
                        parent.insertBefore(clone,password);
                        parent.removeChild(password);
                    }
                }
            }
        </script>
    </body>
    </html>
      

  8.   

    别折腾了, 搞两个东西切换吧。 
    type在某些浏览器上是不允许修改的。
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function showPwd(chk) {
                var $2 = $("#tbPwd,#tbPwd_Text");
                var currVal = $2.filter(":visible").val();
                $2.toggle().val(currVal);
            }
        </script>
    </head>
    <body>
        明文显示:<input type="checkbox" id="CBShowPwd" onchange="showPwd(this)" /><br />
        密码:<input type="password" id="tbPwd"  />
        <input type="text" id="tbPwd_Text" style="display:none;background-color:coral;" />
    </body>
    </html>