$(document).ready(function() {
// #login-box password field
$('#password').attr('type', 'text');
$('#password').val('Password');
});
这是为了将password类型的#password输入字段(id="password")更改为普通的文本字段,然后填充文本"password"。
但这并不奏效。为什么?
表格如下:
<form enctype="application/x-www-form-urlencoded" method="post" action="/auth/sign-in">
<ol>
<li>
<div class="element">
<input type="text" name="username" id="username" value="Prihlasovacie meno" class="input-text" />
</div>
</li>
<li>
<div class="element">
<input type="password" name="password" id="password" value="" class="input-text" />
</div>
</li>
<li class="button">
<div class="button">
<input type="submit" name="sign_in" id="sign_in" value="Prihlásiť" class="input-submit" />
</div>
</li>
</ol>
</form>
更容易……不需要创建所有动态元素。只需创建两个单独的字段,其中一个为“真实”密码字段(type="password"),另一个为“假”密码字段(type="text"),将假字段中的文本设置为浅灰色,并将初始值设置为“password”。然后用jQuery添加几行Javascript代码,如下所示:
<script type="text/javascript">
function pwdFocus() {
$('#fakepassword').hide();
$('#password').show();
$('#password').focus();
}
function pwdBlur() {
if ($('#password').attr('value') == '') {
$('#password').hide();
$('#fakepassword').show();
}
}
</script>
<input style="color: #ccc" type="text" name="fakepassword" id="fakepassword" value="Password" onfocus="pwdFocus()" />
<input style="display: none" type="password" name="password" id="password" value="" onblur="pwdBlur()" />
因此,当用户输入“假”密码字段时,它将被隐藏,真实字段将被显示,焦点将移动到真实字段。他们将永远无法在假字段中输入文本。
当用户离开真实的密码字段时,脚本将查看它是否为空,如果为空,将隐藏真实的字段并显示假的字段。
注意不要在两个输入元素之间留下空格,因为IE会将一个元素置于另一个元素之后(呈现空格),并且当用户进入/退出字段时,字段会出现移动。
作为浏览器安全模型的一部分,很可能会阻止该操作。
编辑:实际上,现在在Safari中测试,我得到的错误类型属性不能更改。
编辑2:这似乎是一个错误直接从jQuery。使用以下直接的DOM代码就可以了:
var pass = document.createElement('input');
pass.type = 'password';
document.body.appendChild(pass);
pass.type = 'text';
pass.value = 'Password';
编辑3:直接从jQuery源代码来看,这似乎与IE有关(可能是一个bug或他们安全模型的一部分,但jQuery不是特定的):
// We can't allow the type property to be changed (since it causes problems in IE)
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
throw "type property can't be changed";
这是所有IE8爱好者的又一个选择,在新浏览器上也能完美运行。您可以只给文本上色以匹配输入的背景。如果你有一个单一的字段,当你点击/聚焦在该字段上时,这将改变颜色为黑色。我不会在公共站点上使用这个,因为它会“迷惑”大多数人,但我在一个只有一个人可以访问用户密码的ADMIN部分中使用它。
$('#MyPass').click(function() {
$(this).css('color', '#000000');
});
-OR-
$('#MyPass').focus(function() {
$(this).css('color', '#000000');
});
这也是需要的,当您离开字段时,将文本更改为白色。简单,简单,简单。
$("#MyPass").blur(function() {
$(this).css('color', '#ffffff');
});
[另一选择]
现在,如果你有几个字段,你正在检查,都有相同的ID,因为我正在使用它,添加一个类的“pass”字段,你想隐藏的文本。设置密码字段类型为“text”。这样,只有类为“pass”的字段才会被更改。
<input type="text" class="pass" id="inp_2" value="snoogle"/>
$('[id^=inp_]').click(function() {
if ($(this).hasClass("pass")) {
$(this).css('color', '#000000');
}
// rest of code
});
这是第二部分。这将在您离开字段后将文本更改为白色。
$("[id^=inp_]").blur(function() {
if ($(this).hasClass("pass")) {
$(this).css('color', '#ffffff');
}
// rest of code
});
使用jQuery的终极方法:
将原始输入字段隐藏在屏幕中。
$("#Password").hide(); //Hide it first
var old_id = $("#Password").attr("id"); //Store ID of hidden input for later use
$("#Password").attr("id","Password_hidden"); //Change ID for hidden input
通过JavaScript创建新的输入字段。
var new_input = document.createElement("input");
将ID和值从隐藏的输入字段迁移到新的输入字段。
new_input.setAttribute("id", old_id); //Assign old hidden input ID to new input
new_input.setAttribute("type","text"); //Set proper type
new_input.value = $("#Password_hidden").val(); //Transfer the value to new input
$("#Password_hidden").after(new_input); //Add new input right behind the hidden input
为了解决IE上的错误,比如类型属性不能被改变,你可能会发现下面的方法很有用:
将click/focus/change事件附加到新的输入元素,以便在隐藏的输入上触发相同的事件。
$(new_input).click(function(){$("#Password_hidden").click();});
//Replicate above line for all other events like focus, change and so on...
旧的隐藏输入元素仍然在DOM中,因此将与新输入元素触发的事件进行反应。当ID交换时,新的输入元素将像旧的输入元素一样,并响应对旧的隐藏输入的ID的任何函数调用,但看起来不同。
有点棘手,但有效!!: -)