DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js修改input的type屬性問題探討
js修改input的type屬性問題探討
編輯:關於JavaScript     
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">

</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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved