我想实现一个显示隐藏密码的功能
代码如下
在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兼容
代码如下
在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兼容
可以新创建个<input>把原来的替换掉
<!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>
为什么不能用呢????
<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>
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>