首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >占位符不在IE中显示密码字段

问占位符不在IE中显示密码字段
EN

Stack Overflow用户
提问于 2014-03-03 07:22:54
回答 2查看 846关注 0票数 2

我已经实现了以下脚本在IE中为输入框显示占位符,但它不适用于密码字段,请通知我。

代码语言:javascript
复制
<script>
 jQuery(function() {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) jQuery.support.placeholder = true;
    });

    // Placeholder for IE
    $(function () {
        if(!$.support.placeholder) { 

            var active = document.activeElement;
            $(':text, textarea').focus(function () {
                if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                    $(this).val('').removeClass('hasPlaceholder');
                }
            }).blur(function () {
                if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                    $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
                }
            });
            $(':text, textarea').blur();
            $(active).focus();
            $('form').submit(function () {
                $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
            });
        }  

    });
</script>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-03 07:25:04

尝试将input[type="password"]添加到选择器中,因此更改:

代码语言:javascript
复制
$(':text, textarea')

至:

代码语言:javascript
复制
$(':text, textarea, input[type="password"]')

因为*文本选择器只选择<input type="text">元素

票数 2
EN

Stack Overflow用户

发布于 2014-03-03 07:28:27

两个问题:

  1. 您没有选择密码字段。:text jQuery选择器只选择type=text (显式或隐式)的input。您必须将input[type=passsword]添加到选择器中,才能同时包含密码字段。
  2. 但是,一旦修复了这个问题,当然,密码字段的值就会被模糊显示。因此,在它上设置占位符文本不会显示占位符。您必须以其他方式显示占位符文本,例如覆盖在字段上的span或div。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22140888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档