04
2019
06

ie8(ie9)兼容placeholder

function placeHolderSupport(){
    var s = document.createElement('input'),
    isSupport = 'placeholder' in s;
    
    if ( !isSupport ) {
        jQuery(':text').each(function(){
            if (typeof jQuery(this).attr('placeholder') != 'undefined') {
                var $this = jQuery(this),
                    p = $this.parent(),
                    spt = $this.css('padding-top'),
                    spl = $this.css('padding-left'),
                    spfs = $this.css('font-size'),
                    placeTxt = $this.attr('placeholder');

                if (p.find('.placeholder').length != 0) {
                    return true;
                }

                if (p.css('position') == 'static') {
                    p.css('position', 'relative');
                }
                p.append('<span class="placeholder" style="position:absolute;left:'+ spl +';top:'+ spt +';font-size:'+ spfs +';">'+ placeTxt +'</span>');
                if ($this.val() != '') {
                    $this.parent().find('.placeholder').hide();
                }
                $this.on({
                    'focus': function(){
                        $this.parent().find('.placeholder').hide();
                    },
                    'blur': function(){
                        if ($this.val() == '') {
                            $this.parent().find('.placeholder').show();
                        } else {
                            $this.parent().find('.placeholder').hide();
                        }
                    }
                });
                $this.parent().find('.placeholder').on('click', function(){
                    jQuery(this).hide();
                    $this.focus();
                });
            }
        });
    }
}


« 上一篇 下一篇 »

公告:

跟着我一起每天进步一点点,让学习成为一种习惯!

公告:

跟着我一起每天进步一点点,让学习成为一种习惯!