2011-05-09 89 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