javascript - 占位符和 IE != BFF(占位符和 IE9 不起作用)

标签 javascript html css internet-explorer placeholder

所以,我试图让占位符属性在 IE9 及更低版本中工作。但是,我的行为很奇怪(请参见下面的屏幕截图)。

我找到了this website并决定使用下面的JS代码:

JS:

    // Checks browser support for the placeholder attribute
    jQuery(function() {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) jQuery.support.placeholder = true;
    });

    // Placeholder for IE
    $(function () {
        if(!$.support.placeholder) { 

            var active = document.activeElement;
            $(':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');
                }
            });
            $(':text, textarea').blur();
            $(active).focus();
            $('form').submit(function () {
                $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
            });
        }  

    });

HTML:

    <p>
        <label>To: </label>
        <input type="text"  id="toEmail" placeholder="Recipient's Email Address" />
    </p>       

    <p>
        <label>From:</label>
        <input type="text" id="fromName" placeholder="Your Name" />
    </p>

    <p>
        <label>From: </label>
        <input type="text" id="fromEmail" placeholder="Your Email Address" />
    </p>

    <p>
        <label>Message:</label>
        <textarea rows="5" cols="5" id="messageEmail"></textarea>
    </p>

CSS:

.hasPlaceholder {
    color: #999;
}

我的网站打开一个带有表单的模式。但是,当首次打开模式时,不会显示任何占位符文本:

Initial State

但是,如果我单击文本字段,然后单击文本字段外,则会显示占位符文本。

After Onblur of text field

我希望在模式打开后立即显示文本。原来如此……

仅供引用- 我怀疑文本最初可能不会出现,因为我的模式最初是隐藏的。如果是这种情况我该如何解决?

注意:我知道插件的存在。我不想使用插件。

最佳答案

将你的jquery更改为:

; (function ($) {
    $.fn.placehold = function (placeholderClassName) {
        var placeholderClassName = placeholderClassName || "placeholder",
            supported = $.fn.placehold.is_supported();

        function toggle() {
            for (i = 0; i < arguments.length; i++) {
                arguments[i].toggle();
            }
        }

        return supported ? this : this.each(function () {
            var $elem = $(this),
                placeholder_attr = $elem.attr("placeholder");

            if (placeholder_attr) {
                if ($elem.val() === "" || $elem.val() == placeholder_attr) {
                    $elem.addClass(placeholderClassName).val(placeholder_attr);
                }

                if ($elem.is(":password")) {
                    var $pwd_shiv = $("<input />", {
                        "class": $elem.attr("class") + " " + placeholderClassName,
                        "value": placeholder_attr
                    });

                    $pwd_shiv.bind("focus.placehold", function () {
                        toggle($elem, $pwd_shiv);
                        $elem.focus();
                    });

                    $elem.bind("blur.placehold", function () {
                        if ($elem.val() === "") {
                            toggle($elem, $pwd_shiv);
                        }
                    });

                    $elem.hide().after($pwd_shiv);
                }

                $elem.bind({
                    "focus.placehold": function () {
                        if ($elem.val() == placeholder_attr) {
                            $elem.removeClass(placeholderClassName).val("");
                        }
                    },
                    "blur.placehold": function () {
                        if ($elem.val() === "") {
                            $elem.addClass(placeholderClassName).val(placeholder_attr);
                        }
                    }
                });

                $elem.closest("form").bind("submit.placehold", function () {
                    if ($elem.val() == placeholder_attr) {
                        $elem.val("");
                    }

                    return true;
                });
            }
        });
    };

    $.fn.placehold.is_supported = function () {
        return "placeholder" in document.createElement("input");
    };
})(jQuery);

然后让函数工作:

$("input, textarea").placehold("something-temporary");

关于javascript - 占位符和 IE != BFF(占位符和 IE9 不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963744/

相关文章:

javascript - 如何替换一个字符串中的多个字符

javascript - 在 android 中引用 javax.script.ScriptEngine 或计算 javascript 表达式

html - 如何应用背景颜色

html - 如何使用百分比调整嵌入的 html 对象的高度?

css - 有没有办法设置独立 TableView 列的样式?

javascript - 动态生成的页面向下滚动

Javascript (jQuery) 闪光效果 : how to do that?

css - 将文本直接放在多个图像下方

javascript - 为了在 View 中获取 API 数据,应采用哪个值?

javascript - 如何将发布的值放入文本框中