js修改input的type属性问题探讨
来源:互联网 发布:wav整轨转分轨软件 编辑:程序博客网 时间:2024/06/05 05:49
js修改input的type属性有些限制。当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题。但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改。在ff下仍是可读写属性。
今天遇到个问题,输入框有默认值“密码”,但获得焦点时,“密码”两字会去掉,输入时直接变成”****“的password类型。很明显,一开始的时候,input的类型是text,后来变成了password类型。直观的思路是用js修改input的type类型。但ie下这么做不可行,所以只能换个思路,写两个input,一个text类型,一个password类型,分得监听onfocus和onblur事件。如下:
注意:script那段代码要写到html里面
代码如下:
<!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">
unity3d专题 http://www.unitymanual.com/
</style>
<body>
<input name="" type="text" value="密码" class="inputText_1" id="tx" style="width:100px;" />
<input name="" type="password" style="display:none;width: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>
今天遇到个问题,输入框有默认值“密码”,但获得焦点时,“密码”两字会去掉,输入时直接变成”****“的password类型。很明显,一开始的时候,input的类型是text,后来变成了password类型。直观的思路是用js修改input的type类型。但ie下这么做不可行,所以只能换个思路,写两个input,一个text类型,一个password类型,分得监听onfocus和onblur事件。如下:
注意:script那段代码要写到html里面
代码如下:
<!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">
unity3d专题 http://www.unitymanual.com/
</style>
<body>
<input name="" type="text" value="密码" class="inputText_1" id="tx" style="width:100px;" />
<input name="" type="password" style="display:none;width: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>
- js修改input的type属性问题探讨
- JS 动态修改 input 的 type 属性
- JS 动态修改 input 的 type 属性
- 修改input输入框的type属性
- js改变input的type属性
- 修改input的type属性的几种方法
- 【CRM项目】jQuery修改input的type属性失败
- js无法强制修改input type=file的value
- js为修改input属性
- 更改input的type属性
- jQuery修改input输入框type属性时报错的处理
- HTML <input> 标签的 type 属性
- HTML <input> 标签的 type 属性
- html5 input的type属性启动输入法
- input中type的属性和作用
- HTML <input> 标签的 type 属性
- Html5中input标记的type属性
- <input>的type属性
- CheckStyle unable to read from stream
- C++中const成员函数
- UIView部分圆角设置
- Python开发环境Wing IDE如何检查Python集成
- Bat 脚本 学习记录一
- js修改input的type属性问题探讨
- ARM和x86的区别
- 算法习题9:判断整数序列是不是二元查找树的后序遍历结果
- .Net调用Office Com组件的原理及问题
- UISlider 滑块控件—IOS开发
- php文件加锁 lock_sh ,lock_ex
- Spring AOP介绍及源码分析
- 产品经理自我修养
- Outlook 2007使用IMAP收邮件只有收件箱解决办法