【问题标题】:How to set placeholder for input box in IE如何在IE中为输入框设置占位符
【发布时间】:2011-12-13 22:21:22
【问题描述】:

我正在尝试为 IE 中的输入框设置占位符。但它没有用......

<script>
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) jQuery.support.placeholder = true;
});
// This adds placeholder support to browsers that wouldn't otherwise support it.
$(function() {
    if(!$.support.placeholder) { 
        var active = document.activeElement;
        $(':text').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').blur();
        $(active).focus();
        $('form:eq(0)').submit(function () {
            $(':text.hasPlaceholder').val('');
        });
    }
});

我正在使用 jQuery 来执行此操作。第一个我定义了“占位符”,第二个我添加了对浏览器的占位符支持。

【问题讨论】:

标签: internet-explorer placeholder


【解决方案1】:

我不确定,但如果您使用框架来执行此操作,您会遇到document.activeElement 的问题。尝试对此发表评论并致电$(active).focus();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-31
    • 2013-09-01
    • 2014-06-21
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2016-12-19
    相关资源
    最近更新 更多