2012-11-16 88 views
3

我使用的,而不是HTML5占位符属性的jQuery型密码jQuery的输入占位符,在IE8

<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 = ''; }" /> 

这是工作的罚款type="text"type="password"它仅显示*

如何我将使用占位符作为密码字段?需要出IE8

先谢谢了答案......

+1

[占位符.js](https://github.com/jamesallardice/Placeholders.js)可以在除IE8及以下版本之外的所有浏览器中使用'password'输入类型正确运行(并且还可以改进当前解决方案,方法是不提交占位符值表格)。您不能在IE8及以下版本中完成,而无需临时创建新元素。 –

+0

可能重复的http://stackoverflow.com/questions/6052544/showing-placeholder-text-for-password-field-in-ie –

回答

14

我创建了一个普通的输入,并在focus切换到一个密码字段,如果值是blur空,然后再切换回一个正常输入,否则将其作为密码字段。

http://jsfiddle.net/q8ajJ/Here is a working JSFiddle:

的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"/>​ 

的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) 
}); 

+0

谢谢老兄..这种方法在所有浏览器都能正常工作... – Dhamu

+1

那就好了,我有这么多次这个问题!,这是我发现所有浏览器的最佳解决方案! – Anil

6

Here是另一个肮脏的黑客 - 你可以创建labelposition: absolute和显示/隐藏与焦点和模糊JS。

据我所知,它适用于任何浏览器。此外,验证表单提交更为简单。

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

div.wrapper {position: relative} 
label.placeholder {position: absolute; color: #CCC; left: 2px; top: 0} 

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(); 
     } 
    } 
}); 
+0

这种方法对于IE +浏览器来说非常完美,特别是它不会干扰任何验证。请注意,您将不得不调整标签占位符样式,以便它能够与当前的登录表单一起分享,并且它实际上应该覆盖输入类型密码字段的长度。 :) –

4

基于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(); 
      } 
     }); 
    }); 
} 

然后,所有你需要做的就是调用

ManagePasswords($('#rootElement')); 

希望这有助于!

1

为占位符添加另一个input元素可能会在form验证和提交期间创建问题。我用不同的方式解决了这个问题。

的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 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(); 
      } 
     });     
    });   
} 

现在只是调用函数的形式。

placeholder($("#test")); 

这将与占位符,包括type="password"在IE8,IE9,IE10,谷歌Chrome,火狐,Safari和Opera测试的所有类型的输入领域的工作。

1

使用此代码,jQuery将做休息......

HTML代码替换

<!--[if IE 8]><html class="ie8"><![endif]--> 
<!--[if gt IE 8]><html><![endif]--> 

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> 
.ie8Lbls { 
    font-size:10px; 
} 
</style>