JQ無法修改input的type屬性的替代解決方法


需要實現的效果:一個輸入框,當輸入框未獲得焦點的時候,顯示為 “請輸入密碼”;當輸入內容並失去焦點的時候,輸入內容顯示為”*****”,如果沒有輸入仍然顯示“請輸入密碼”;

方法一:使用text,隱藏域中,然后配合onkeypress、onkeyup、focus、blur等事件基本可以達到要求,此種方法比較麻煩;

方法二:用text和password一起  

html: 

1 <input type="password" id="input_password" class="m_input_se1 leftd" value='' style="display:none;color:#444;"/>
2 <input type="text" id="showPwd" class="m_input_se1 leftd" value="請輸入密碼" style="color:#c0c0c0"/>

js:

 1 $("#showPwd").focus(function() {
 2     var text_value = $(this).val();
 3     if (text_value == "請輸入密碼") {
 4           $("#showPwd").hide();
 5         $("#input_password").show().focus();
 6     }
 7 });
 8 $("#input_password").blur(function() {
 9     var text_value = $(this).val();
10     if (text_value == "") {
11         $("#showPwd").show();
12         $("#input_password").hide();
13     }
14 });

至此完美解決,所有瀏覽器都可以使用。

 

擴展內容:

本來想使用JQ的attr修改type屬性值,但是發現在IE上會出錯,如下:uncaught exception type property can't be changed

查看jQuery 1.42源碼 1488 行

// We can't allow the type property to be changed (since it causes problems in IE)
if ( name === “type” && rtype.test( elem.nodeName ) && elem.parentNode ) {
  jQuery.error( “type property can't be changed” );
}

沒辦法,IE不支持修改type屬性。


免責聲明!

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



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