js修改input的type屬性


http://www.oschina.net/question/1378350_128616

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> 

 

直接用span標簽讓它固定在密碼框上面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM