javascript - 占位符未在 IE 中显示密码字段

标签 javascript jquery html css

我已经实现了以下脚本来在 IE 中显示输入框的占位符,但它不适用于密码字段,请建议我..

<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>

最佳答案

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

$(':text, textarea')

至:

$(':text, textarea, input[type="password"]')

:text selector 仅选择<input type="text">元素

关于javascript - 占位符未在 IE 中显示密码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22140888/

相关文章:

javascript - 如果表格单元格的文本与变量相同,则更改表格单元格的 CSS

开放层中的 Javascript "unspecified error"

javascript - Bootstrap Accordion 效果不佳

html - CSS媒体打印背景图片

html - CSS 的正文背景图像不起作用

javascript - 阻止网站请求外部src

javascript - react css 优先级

javascript - 使事件在第一次单击的元素上发生,但仅当单击另一个元素时才再次触发该事件

jQuery radio onchange 切换父元素的类?

HTML anchor 标记