IE 8-不支持 placeholder 解决方法

        ;!function fixPlaceholder() {
            var hasPlaceholder = 'placeholder' in document.createElement('input');
            if(!hasPlaceholder) {
                $(document).on('blur', '[placeholder]', function () {
                    if($(this).val() == '') {
                        $(this).val($(this).attr('placeholder'))
                    }
                }).on('focus', '[placeholder]', function () {
                    var $this = $(this);
                    if($this.val() == $this.attr('placeholder')) {
                        var r = $this.createTextRange();
                        r.collapse(true);
                        r.select();
                        if (typeof($this.onselectstart) != "undefined") {
                            // IE下禁止元素被选取
                            $this.onselectstart = new Function("return false");
                        }
                    }

                }).on('keydown','[placeholder]', function () {
                    var $this = $(this);
                    if($this.val() == $this.attr('placeholder')) {
                        $this.val('');
                    }

                    if (typeof($this.onselectstart) != "undefined") {
                        // IE下禁止元素被选取
                        $this.onselectstart = new Function("return false");
                    }
                });

                $('[placeholder]').blur();
            }
        }();

posted @ 2017-03-22 11:12  Liaofy  阅读(288)  评论(0编辑  收藏  举报