简体   繁体   中英

Placeholder in Internet Explorer 8 & 9

Guys I know this question has been asked here many time but still I wasn't able to find a solution to my problem. I have a textarea in my asp.net mvc application which has a placeholder in it.

<textarea placeholder="Write Query..." maxlength="1000"></textarea>

Now Internet Explorer 8 & 9 doesn't support the placeholder property so I need some workaround for it, I have search here and google and found various javascripts but unfortunately none of those worked. Although some worked (as the showed the placeholder text) but the text didn't disappear on writing in the textArea

Two of those scripts (that worked half right) are these :

$(function () {
    if (!$.support.placeholder) {
        var active = document.activeElement;
        $('input[type="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');
            }
        });
        $('input[type="text"], textarea').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function () { $(this).val(''); });
        });
    }
});

Second :

$(function () {
    if ($.browser.msie && $.browser.version <= 9) {
        $("[placeholder]").focus(function () {
            if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
        }).blur(function () {
            if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
        }).blur();

        $("[placeholder]").parents("form").submit(function () {
            $(this).find('[placeholder]').each(function () {
                if ($(this).val() == $(this).attr("placeholder")) {
                    $(this).val("");
                }
            })
        });
    }
});

try to this alternative

https://github.com/parndt/jquery-html5-placeholder-shim/

i think it's can manage by meta tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM