HTML5 placeholder在低版本浏览器的解决方法

HTML5 属性placeholder:提供可描述输入字段预期的提示信息。该提示会在输入字段为空显示,并会在字段获取焦点时消失,并且val里的值为空。

但是placeholder属性在ie9及以下浏览器不支持我的解决方案是:

判断浏览器是否支持placeholder属性,如果不支持就获取所有placeholder属性的标签,进行赋值,但是password需要转换为text才能显示文本信息,于是我就多做了一层判断

function placeholderSupport() {
                        return 'placeholder' in document.createElement('input');
                    }
                    $(function () {
                        if (!placeholderSupport()) {   // 判断浏览器是否支持 placeholder
                            $("input[placeholder]").each(function (k, v) {
                                if ($(v).attr("type") == "text") {
                                    $(v).val($(v).attr("placeholder"));
                                    $(v).focus(function () {
                                        if ($(v).val() == $(v).attr("placeholder") || $(v).val() == "") {
                                            $(v).val("");
                                        }
                                    }).blur(function () {
                                        //$(v).val($(v).attr("placeholder"));
                                        if ($(v).val() == $(v).attr("placeholder") || $(v).val() == "") {
                                            $(v).val($(v).attr("placeholder"));
                                        }
                                    });
                                } else if ($(v).attr("type") == "password") {
                                    $(v).attr("type", "text");
                                    $(v).val($(v).attr("placeholder"));
                                    $(v).focus(function () {
                                        if ($(v).val() == $(v).attr("placeholder") || $(v).val() == "") {
                                            $(v).attr("type", "password");
                                            $(v).val("");
                                        }
                                    }).blur(function () {
                                        if ($(v).val() == $(v).attr("placeholder") || $(v).val() == "") {
                                            $(v).attr("type", "text");
                                            $(v).val($(v).attr("placeholder"));
                                        }
                                    });
                                }
                            });

                        };
                    })


你可能感兴趣的:(web前端)