IE兼容-placeholder的方法

由于IE8以下的浏览器不支持placeholder。
然后通常解决浏览器兼容的问题都是两种思路嘛,一种就是平滑下降,去掉功能。另一种就是写一个(或者找一个)实现这个功能的库。
而因为交互的原因,使用placeholder后,如果删去,对网站的功能会造成很大的影响,所以也只好用写库代替的方法了。

一、首先是网上摘录的一个自动填写value的方法
$(function(){

       handlePlaceholderForIE();
});
//原始从网上摘录的方法
 function handlePlaceholderForIE() {
            // placeholder attribute for ie7 & ie8
            if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 9) {

                // ie7&ie8
                jQuery('input[placeholder], textarea[placeholder]').each(function () {
                    var input = jQuery(this);
                    jQuery(input).val(input.attr('placeholder'));
                    jQuery(input).focus(function () {
                        if (input.val() == input.attr('placeholder')) {
                            input.val('');
                        }
                    });
                    jQuery(input).blur(function () {
                        if (input.val() == '' || input.val() == input.attr('placeholder')) {
                            input.val(input.attr('placeholder'));
                        }
                    });
                });
            }
        }

然后这个方法会有缺陷,就是如果input是密码的话,则填写上去的内容会是两个小黑点

二、知乎的方法

记忆中,知乎的方法是在placeholder附近加上一个带文字的label,然后在密码的input中,使用label覆盖上去,代替value文字。根据这个思路,修改代码为:

//修改版后
function handlePlaceholderForIE() {
            // placeholder attribute for ie7 & ie8
            if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 9) {

                // ie7&ie8
                jQuery('input[placeholder], textarea[placeholder]').each(function () {
                    var input = jQuery(this);
                    if(input.attr("type") == "password"){
                        $("#txtPwd_l").show()
                    }else{
                        jQuery(input).val(input.attr('placeholder'));
                    }
                    jQuery(input).focus(function () {
                        if(input.attr("type") == "password"){
                            $("#txtPwd_l").hide();
                        }
                        if (input.val() == input.attr('placeholder')) {
                            input.val('');
                        }

                    });
                    jQuery(input).blur(function () {
                            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                                if(input.attr("type") == "password"){
                                    $("#txtPwd_l").show()
                                }else{
                                    input.val(input.attr('placeholder'));
                                }
                            }

                    });
                });
            }
        }
//<input class="fxnloginnew" id="prePwd" style="background:#fff\0;" type="password" value="" placeholder="密码"><label id="txtPwd_l" style="display:none;  position: absolute;top: 35px;  left: 10px;color:#898989;">密码</label>
三、最终方法

然而为了使用方便,觉得可以写一个js,直接依靠js来生成label,这样就可以更加傻瓜的使用这个函数了。
//因此最终觉得可以写一个JS的插件
//然而因为我懒,还没有写出来,哈哈哈哈哈哈
//by 2015.8.12

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值