如何在IE中为输入框设置占位符

时间:2011-10-24 07:53:09

标签: internet-explorer placeholder

我正在尝试在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来做到这一点。第一个我定义了“占位符”,第二个我添加了浏览器的占位符支持。

1 个答案:

答案 0 :(得分:0)

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