Javascript 密码字段中的Jquery默认值
我使用的是填充输入的文本,这些文本在单击时消失。它对于密码字段并不理想,因为所有内容都显示为点。在开始键入之前,如何使默认文本在密码字段中可见?您可以将输入字段的类型设置为密码。但是,在页面加载时通过javascript将其设置为正常(这样,如果用户没有JS,您可以很容易地回退)。一旦收到点击,将输入字段的类型设置回密码。通过JS,我的答案将与@ultimatebuster的答案相同。然而,由于替代方案开始出现,整个JS路线都很粗糙。许多现代浏览器现在直接通过HTML5支持这一功能:Javascript 密码字段中的Jquery默认值,javascript,jquery,html,forms,input,Javascript,Jquery,Html,Forms,Input,我使用的是填充输入的文本,这些文本在单击时消失。它对于密码字段并不理想,因为所有内容都显示为点。在开始键入之前,如何使默认文本在密码字段中可见?您可以将输入字段的类型设置为密码。但是,在页面加载时通过javascript将其设置为正常(这样,如果用户没有JS,您可以很容易地回退)。一旦收到点击,将输入字段的类型设置回密码。通过JS,我的答案将与@ultimatebuster的答案相同。然而,由于替代方案开始出现,整个JS路线都很粗糙。许多现代浏览器现在直接通过HTML5支持这一功能: <i
<input type="password" name="password" placeholder="Enter password"/>
(许多现代浏览器=除Internet Explorer之外的所有主要浏览器。我拒绝为它编码;如果你必须在IE中也使用相同的东西,你必须走黑客路线。)就像建议的那样,你可以交换输入,但在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 = '';
}
}
});
});
});
我终于放弃了正常的密码输入行为。我发现上面的输入交换有点奇怪,但你可以试一试。好脚本!要解决IE问题,使用show()和hide()在单击并显示时隐藏type=text(0 type=password字段?
placeholder
太棒了!我为您放弃Explorer鼓掌。