2011-05-09 90 views

回答

7

通過JS,我的答案會和@endbuster's一樣。然而,整個JS路線都很黑,現在開始出現替代方案。許多現代瀏覽器現在直接通過HTML5支持這件事情:

<input type="password" name="password" placeholder="Enter password"/> 

(許多現代瀏覽器=除Internet Explorer的每一個主要的一個我拒絕了它的代碼,如果你必須有在IE瀏覽器同樣的事情爲好。 ,你必須走hacky路線。)

+0

'placeholder'太棒了!我讚揚你放棄資源管理器。 – CyberJunkie 2011-05-09 19:28:39

1

您可以將輸入字段的類型設置爲密碼。但是,通過頁面加載時通過JavaScript將其設置爲正常(這樣,如果用戶沒有JS,則可以輕鬆地回退)。一旦收到點擊,將輸入字段的類型設置回密碼。

1

就像建議的那樣,你可以交換輸入,但它在IE中不起作用。 IE不會允許它,因爲它可能是某種安全漏洞。

我用這個:

/* From: http://grzegorz.frydrychowicz.net/jquery_toggleformtext/ 
    Modified to swap password textbox type so watermark can be read */ 
$(document).ready(function() { 
    if (!jQuery.browser.msie) { 
     $("input:password").each(function() { 
      if (this.value == '') { 
       this.type = "text"; 
      } 
      $(this).focus(function() { 
       this.type = "password"; 
      }); 
      $(this).blur(function() { 
       if (this.value == '') { 
        this.type = "text"; 
       } 
      }); 
     }); 
    } 
    $("input:text, textarea, input:password").each(function() { 
     if (this.value == '') { 
      $(this).addClass("watermark"); 
      this.value = this.title; 
     } 
    }); 
    $("input:text, textarea, input:password").focus(function() { 
     $(this).removeClass("watermark"); 
     if (this.value == this.title) { 
      this.value = ''; 
     } 
    }); 
    $("input:text, textarea, input:password").blur(function() { 
     if (this.value == '') { 
      $(this).addClass("watermark"); 
      this.value = this.title; 
     } 
    }); 
    $("input:image, input:button, input:submit").click(function() { 
     $(this.form.elements).each(function() { 
      if (this.type == 'text' || this.type == 'textarea' || this.type == 'password') { 
       if (this.value == this.title && this.title != '') { 
        this.value = ''; 
       } 
      } 
     }); 
    }); 
}); 

我最後只給了一個正常的密碼輸入行爲的去了。我發現上面的輸入交換有點古怪,但你可以嘗試一下。

+0

古德劇本!要解決IE瀏覽器,如何使用show()和hide()隱藏type = text在點擊和顯示(0類型=密碼字段? – CyberJunkie 2011-05-09 19:05:34