繁体   English   中英

Internet Explorer 8和9中的占位符

[英]Placeholder in Internet Explorer 8 & 9

伙计们,我知道这个问题已经在这里问过很多次了,但仍然找不到我的问题的解决方案。 我的asp.net mvc应用程序中有一个textarea ,其中有一个占位符。

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

现在,Internet Explorer 8和9不支持占位符属性,因此我需要一些解决方法,我在这里和google上进行了搜索,发现了各种JavaScript,但不幸的是这些都不起作用。 尽管有些有效(如显示的占位符文本),但在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("");
                }
            })
        });
    }
});

尝试这种替代

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

我认为可以通过meta标签进行管理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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