【问题标题】:jQuery input placeholder for type password in IE8在 IE8 中输入密码的 jQuery 输入占位符
【发布时间】:2012-11-16 10:55:13
【问题描述】:

我使用的是 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 = ''; }"  />

这对type="text" 工作正常,但对于type="password" 它只显示*

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

提前感谢您的回答...

【问题讨论】:

  • Placeholders.js 在除 IE8 及更低版本之外的所有浏览器中都可以与 password 输入类型一起正常工作(并且还通过不随表单提交占位符值来改进您当前的解决方案)。如果不临时创建新元素,则无法在 IE8 及以下版本中执行此操作。

标签: javascript jquery css html


【解决方案1】:

我创建一个普通输入,并在focus 上将其切换到密码字段,如果blur 上的值为空,则将其切换回普通输入,否则将其保留为密码字段。

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

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

【讨论】:

    【解决方案2】:

    Here 是另一个肮脏的 hack - 您可以使用 position: absolute 创建 label 并使用 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();
            }
        }
    });
    

    【讨论】:

    • 这种方法非常适合 IE+ 浏览器,尤其是它不会干扰任何验证。请注意,您必须调整标签占位符样式,以便它与您当前的登录表单很好地配合,并且它实际上应该覆盖您输入类型密码字段的长度。 :)
    【解决方案3】:

    根据 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'));
    

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      为占位符添加另一个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"));
      

      这适用于所有类型的带占位符的输入字段,包括在 IE8、IE9、IE10、Google Chrome、FireFox、Safari 和 Opera 中测试的 type="password"

      【讨论】:

        【解决方案5】:

        使用这段代码,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>
        

        【讨论】:

          猜你喜欢
          • 2012-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-01
          • 1970-01-01
          • 2012-09-27
          相关资源
          最近更新 更多