核心提示:input标签value与password属性不能共存的解决方法,很多时候,我们在处理Input的时候会遇到以下情景:一个input type需要时password 但是又要在加载之后input里有请...
input标签value与password属性不能共存的解决方法,很多时候,我们在处理Input的时候会遇到以下情景:
一个input type需要时password 但是又要在加载之后input里有“请输入密码”这样的提示,但是当type设置成了password之后,value属性是失效的。
js修改input的type属性有些限制。当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题。但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改。在ff下仍是可读写属性。
输入框有默认值“密码”,但获得焦点时,“密码”两字会去掉,输入时直接变成”**“的password类型。很明显,一开始的时候,input的类型是text,后来变成了password类型。直观的思路是用js修改input的type类型。但ie下这么做不可行,所以只能换个思路,写两个input,一个text类型,一个password类型,分得监听onfocus和onblur事件。
<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <input name="" type="text" value="密码" id="tx" /> <input name="" type="password" 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>
当然,如果你懂html得话 placeholder可以一下搞定!