http://www.oschina.net/question/1378350_128616javascript
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裏面
代碼以下:
unity3d http://www.unitymanual.com
<!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="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> css
直接用span標籤讓它固定在密碼框上面html