Internet Explorer 8中的占位符& 9

时间:2014-07-15 15:57:44

标签: javascript jquery asp.net-mvc

伙计们我知道这个问题已在很多时候被问过,但我仍无法找到问题的解决方案。我的asp.net mvc应用程序中有一个textarea,其中有一个占位符。

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

现在Internet Explorer 8&amp; 9不支持占位符属性所以我需要一些解决方法,我在这里搜索和谷歌,发现各种javascripts但不幸的是没有一个工作。虽然有些工作(如显示占位符文本)但文字在textArea

写作时没有消失

其中两个脚本(右半边)是:

$(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(''); });
        });
    }
});

第二:

$(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("");
                }
            })
        });
    }
});

1 个答案:

答案 0 :(得分:0)

尝试这种替代方案

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

我认为它可以通过元标记管理