[英]jQuery input placeholder for type password in IE8
I am using jQuery instead of HTML5 placeholder attribute 我使用的是jQuery而不是HTML5占位符属性
<input type="text" name="email" value="Email" onfocus="if (this.value == 'Email') { this.value = ''; }" onblur="if (this.value == '') { this.value = 'Email'; }" onclick="if (this.value == 'Email') { this.value = ''; }" />
this is working fine for type="text"
but for type="password"
it show only * 这对于type="text"
工作正常,但对于type="password"
它只显示*
How i will use placeholder for password field? 我将如何使用占位符密码字段? Need out in IE8 需要在IE8中
Advance thanks for answers... 预谢谢答案......
I create a normal input, and on focus
switch it to a password field, and if the value is empty on blur
, then switch it back to a normal input, else leave it as the password field. 我创建一个普通输入,并在focus
上将其切换到密码字段,如果blur
时该值为空,则将其切换回正常输入,否则将其保留为密码字段。
Here is a working JSFiddle: http://jsfiddle.net/q8ajJ/ 这是一个有效的JSFiddle: http : //jsfiddle.net/q8ajJ/
The HTML HTML
<!-- Style = display none for people who dont have javascript -->
<input type="text" name="fake_pass" id="fake_pass" value="Enter Password:" style="display:none"/>
<input type="password" name="real_pass" id="real_pass"/>
The Javascript (jQuery) Javascript(jQuery)
// On DOM ready, hide the real password
$('#real_pass').hide();
// Show the fake pass (because JS is enabled)
$('#fake_pass').show();
// On focus of the fake password field
$('#fake_pass').focus(function(){
$(this).hide(); // hide the fake password input text
$('#real_pass').show().focus(); // and show the real password input password
});
// On blur of the real pass
$('#real_pass').blur(function(){
if($(this).val() == ""){ // if the value is empty,
$(this).hide(); // hide the real password field
$('#fake_pass').show(); // show the fake password
}
// otherwise, a password has been entered,
// so do nothing (leave the real password showing)
});
Here is another dirty hack - you can create label
with position: absolute
and show/hide it with JS on focus and blur. 这是另一个肮脏的黑客 - 您可以使用position: absolute
创建label
position: absolute
并使用JS在焦点和模糊时显示/隐藏它。
As far as I know, it works in any browser. 据我所知,它适用于任何浏览器。 Moreover, it is more simple to validate form submitting. 而且,验证表单提交更简单。
HTML HTML
<div class="wrapper">
<input class="withPlaceholder" type="password" id="pass" />
<label class="placeholder" for="pass">Password</label>
</div>
<br />
<div class="wrapper">
<input class="withPlaceholder" type="password" id="pass2" />
<label class="placeholder" for="pass2">Password2</label>
</div>
CSS CSS
div.wrapper {position: relative}
label.placeholder {position: absolute; color: #CCC; left: 2px; top: 0}
JS JS
$("input.withPlaceholder").on({
focus: function() {
$("label[for=" + $(this).prop("id") + "]").hide();
},
blur: function() {
if ($(this).val().length == 0) {
$("label[for=" + $(this).prop("id") + "]").show();
}
}
});
Based on JustAnil's answer, and looking for a way to avoid having to manually add each corresponding for every password fields, I came up with this adaptation of JustAnil's solution: 根据JustAnil的回答,并寻找一种方法来避免为每个密码字段手动添加每个对应的内容,我想出了JustAnil解决方案的这种改编:
function ManagePasswords(root) {
root.find('input[type=password][placeholder]').each(function (index, current) {
var $current = $(current),
$fake = $current.clone().attr('type', 'text');
$fake.val($fake.attr('placeholder'));
$fake.insertBefore($current);
$current.hide();
$fake.focusin(function () {
$(this).hide();
$(this).next().show().focus();
});
$current.blur(function () {
if ($(this).val() == '') {
$(this).hide();
$(this).prev().show();
}
});
});
}
Then all you have to do is call 然后你所要做的就是打电话
ManagePasswords($('#rootElement'));
Hope this helps! 希望这可以帮助!
Adding another input
element for the placeholder may create trouble during form
validation and submission. 为占位符添加另一个input
元素可能会在form
验证和提交过程中产生麻烦。 I solved this in a different way. 我以不同的方式解决了这个问题。
The HTML HTML
<form id="test">
<input type="text" id="name" name="name" placeholder="Name" />
<input type="password" id="password" name="password" placeholder="Password" />
</form>
jQuery Function jQuery函数
function placeholder(form) {
form.wrapInner('<ul style="list-style: none; list-style-type:none; padding:0; margin: 0;">');
form.find('input[placeholder]').each(function (index, current) {
$(current).css('padding', 0).wrap('<li style="position: relative; list-style: none; list-style-type:none; padding:0; margin: 0;">');
var height = $(current).parent('li').height();
var $current = $(current),
$placeholder = $('<div class="placeholder">'+$current.attr('placeholder')+'</div>');
$placeholder.css({'color': '#AAA', 'position':'absolute', 'top': 0, 'left': 0, 'line-height': height+'px', 'margin': '0 0 0 5px', 'border': '0 none', 'outline': '0 none', 'padding': 0});
$placeholder.insertAfter($current);
$current.removeAttr('placeholder');
$placeholder.click(function(){
$current.focus()
});
$current.keypress(function(){
if($(this).val().length >= 0) {
$placeholder.hide();
}
});
$current.blur(function(){
if($(this).val().length == 0) {
$placeholder.show();
} else {
$placeholder.hide();
}
});
});
}
Now just call function for your form. 现在只需为您的表单调用函数。
placeholder($("#test"));
This will work for all type of input field with placeholder including type="password"
tested in IE8, IE9, IE10, Google Chrome, FireFox, Safari and Opera. 这适用于所有类型的输入字段,其中包含在IE8,IE9,IE10,Google Chrome,FireFox,Safari和Opera中测试的type="password"
占位符。
Use this code, jQuery will do the rest... 使用此代码,jQuery将完成其余的工作......
html tag replace html标签替换
<!--[if IE 8]><html class="ie8"><![endif]-->
<!--[if gt IE 8]><html><![endif]-->
jQuery goodness... jQuery的善良......
$(document).ready(function(){
$('.ie8 [placeholder][type="password"]').each(function(){
$(this).wrap('<div style="position: relative;"></div>');
$('<span style=" position: absolute;top: 5px;left:14px;" class="ie8Lbls">'+$(this).attr('placeholder')+'</span>').insertAfter($(this));
$(this).attr('placeholder','');
if($(this).val() == "") {$(this).parent().find('.ie8Lbls').show();}
}).on('focus',function(){
$(this).parent().find('.ie8Lbls').hide();
}).on('blur',function(){
if($(this).val() == "") {$(this).parent().find('.ie8Lbls').show();}
});
$(document).on('click','.ie8Lbls',function(){
$(this).parent().find('input').focus();
});
});
style to change the looks of the new placeholder 用于更改新占位符外观的样式
<style>
.ie8Lbls {
font-size:10px;
}
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.