javascript - 是什么导致了这个 JS 控制台错误?

标签 javascript jquery

问题是控制台中显示了包含的错误。有人可以帮助我理解为什么会发生这种情况吗?我不久前开始学习 jQuery,但仍然很新。请帮助我指明正确的方向。

“维度:jQuery 集合为空”

(function($) {
var height = $.fn.height,
    width = $.fn.width;
$.fn.extend({
    height: function() {
        if (!this[0]) error();
        if (this[0] == window)
            if (($.browser.mozilla || $.browser.opera) && $(document).width() > self.innerWidth) return self.innerHeight - getScrollbarWidth();
            else
                return self.innerHeight || $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
        if (this[0] == document) return Math.max(document.body.scrollHeight, document.body.offsetHeight);
        return height.apply(this, arguments);
    },
    width: function() {
        if (!this[0]) error();
        if (this[0] == window)
            if (($.browser.mozilla || $.browser.opera) && $(document).height() > self.innerHeight) return self.innerWidth - getScrollbarWidth();
            else
                return self.innerWidth || $.boxModel && document.documentElement.clientWidth || document.body.clientWidth;
        if (this[0] == document)
            if ($.browser.mozilla) {
                var scrollLeft = self.pageXOffset;
                self.scrollTo(99999999, self.pageYOffset);
                var scrollWidth = self.pageXOffset;
                self.scrollTo(scrollLeft, self.pageYOffset);
                return document.body.offsetWidth + scrollWidth;
            } else
                return Math.max(document.body.scrollWidth, document.body.offsetWidth);
        return width.apply(this, arguments);
    },
    innerHeight: function() {
        if (!this[0]) error();
        return this[0] == window || this[0] == document ? this.height() : this.is(':visible') ? this[0].offsetHeight - num(this, 'borderTopWidth') - num(this, 'borderBottomWidth') : this.height() + num(this, 'paddingTop') + num(this, 'paddingBottom');
    },
    innerWidth: function() {
        if (!this[0]) error();
        return this[0] == window || this[0] == document ? this.width() : this.is(':visible') ? this[0].offsetWidth - num(this, 'borderLeftWidth') - num(this, 'borderRightWidth') : this.width() + num(this, 'paddingLeft') + num(this, 'paddingRight');
    },
    outerHeight: function(options) {
        if (!this[0]) error();
        options = $.extend({
            margin: false
        }, options || {});
        return this[0] == window || this[0] == document ? this.height() : this.is(':visible') ? this[0].offsetHeight + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0) : this.height() + num(this, 'borderTopWidth') + num(this, 'borderBottomWidth') + num(this, 'paddingTop') + num(this, 'paddingBottom') + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0);
    },
    outerWidth: function(options) {
        if (!this[0]) error();
        options = $.extend({
            margin: false
        }, options || {});
        return this[0] == window || this[0] == document ? this.width() : this.is(':visible') ? this[0].offsetWidth + (options.margin ? (num(this, 'marginLeft') + num(this, 'marginRight')) : 0) : this.width() + num(this, 'borderLeftWidth') + num(this, 'borderRightWidth') + num(this, 'paddingLeft') + num(this, 'paddingRight') + (options.margin ? (num(this, 'marginLeft') + num(this, 'marginRight')) : 0);
    },
    scrollLeft: function(val) {
        if (!this[0]) error();
        if (val != undefined) return this.each(function() {
            if (this == window || this == document) window.scrollTo(val, $(window).scrollTop());
            else
                this.scrollLeft = val;
        });
        if (this[0] == window || this[0] == document) return self.pageXOffset || $.boxModel && document.documentElement.scrollLeft || document.body.scrollLeft;
        return this[0].scrollLeft;
    },
    scrollTop: function(val) {
        if (!this[0]) error();
        if (val != undefined) return this.each(function() {
            if (this == window || this == document) window.scrollTo($(window).scrollLeft(), val);
            else
                this.scrollTop = val;
        });
        if (this[0] == window || this[0] == document) return self.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;
        return this[0].scrollTop;
    },
    position: function(returnObject) {
        return this.offset({
            margin: false,
            scroll: false,
            relativeTo: this.offsetParent()
        }, returnObject);
    },
    offset: function(options, returnObject) {
        if (!this[0]) error();
        var x = 0,
            y = 0,
            sl = 0,
            st = 0,
            elem = this[0],
            parent = this[0],
            op, parPos, elemPos = $.css(elem, 'position'),
            mo = $.browser.mozilla,
            ie = $.browser.msie,
            oa = $.browser.opera,
            sf = $.browser.safari,
            sf3 = $.browser.safari && parseInt($.browser.version) > 520,
            absparent = false,
            relparent = false,
            options = $.extend({
                margin: true,
                border: false,
                padding: false,
                scroll: true,
                lite: false,
                relativeTo: document.body
            }, options || {});
        if (options.lite) return this.offsetLite(options, returnObject);
        if (options.relativeTo.jquery) options.relativeTo = options.relativeTo[0];
        if (elem.tagName == 'BODY') {
            x = elem.offsetLeft;
            y = elem.offsetTop;
            if (mo) {
                x += num(elem, 'marginLeft') + (num(elem, 'borderLeftWidth') * 2);
                y += num(elem, 'marginTop') + (num(elem, 'borderTopWidth') * 2);
            } else
            if (oa) {
                x += num(elem, 'marginLeft');
                y += num(elem, 'marginTop');
            } else
            if ((ie && jQuery.boxModel)) {
                x += num(elem, 'borderLeftWidth');
                y += num(elem, 'borderTopWidth');
            } else
            if (sf3) {
                x += num(elem, 'marginLeft') + num(elem, 'borderLeftWidth');
                y += num(elem, 'marginTop') + num(elem, 'borderTopWidth');
            }
        } else {
            do {
                parPos = $.css(parent, 'position');
                x += parent.offsetLeft;
                y += parent.offsetTop;
                if (mo || ie || sf3) {
                    x += num(parent, 'borderLeftWidth');
                    y += num(parent, 'borderTopWidth');
                    if (mo && parPos == 'absolute') absparent = true;
                    if (ie && parPos == 'relative') relparent = true;
                }
                op = parent.offsetParent || document.body;
                if (options.scroll || mo) {
                    do {
                        if (options.scroll) {
                            sl += parent.scrollLeft;
                            st += parent.scrollTop;
                        }
                        if (oa && ($.css(parent, 'display') || '').match(/table-row|inline/)) {
                            sl = sl - ((parent.scrollLeft == parent.offsetLeft) ? parent.scrollLeft : 0);
                            st = st - ((parent.scrollTop == parent.offsetTop) ? parent.scrollTop : 0);
                        }
                        if (mo && parent != elem && $.css(parent, 'overflow') != 'visible') {
                            x += num(parent, 'borderLeftWidth');
                            y += num(parent, 'borderTopWidth');
                        }
                        parent = parent.parentNode;
                    } while (parent != op);
                }
                parent = op;
                if (parent == options.relativeTo && !(parent.tagName == 'BODY' || parent.tagName == 'HTML')) {
                    if (mo && parent != elem && $.css(parent, 'overflow') != 'visible') {
                        x += num(parent, 'borderLeftWidth');
                        y += num(parent, 'borderTopWidth');
                    }
                    if (((sf && !sf3) || oa) && parPos != 'static') {
                        x -= num(op, 'borderLeftWidth');
                        y -= num(op, 'borderTopWidth');
                    }
                    break;
                }
                if (parent.tagName == 'BODY' || parent.tagName == 'HTML') {
                    if (((sf && !sf3) || (ie && $.boxModel)) && elemPos != 'absolute' && elemPos != 'fixed') {
                        x += num(parent, 'marginLeft');
                        y += num(parent, 'marginTop');
                    }
                    if (sf3 || (mo && !absparent && elemPos != 'fixed') || (ie && elemPos == 'static' && !relparent)) {
                        x += num(parent, 'borderLeftWidth');
                        y += num(parent, 'borderTopWidth');
                    }
                    break;
                }
            } while (parent);
        }
        var returnValue = handleOffsetReturn(elem, options, x, y, sl, st);
        if (returnObject) {
            $.extend(returnObject, returnValue);
            return this;
        } else {
            return returnValue;
        }
    },
    offsetLite: function(options, returnObject) {
        if (!this[0]) error();
        var x = 0,
            y = 0,
            sl = 0,
            st = 0,
            parent = this[0],
            offsetParent, options = $.extend({
                margin: true,
                border: false,
                padding: false,
                scroll: true,
                relativeTo: document.body
            }, options || {});
        if (options.relativeTo.jquery) options.relativeTo = options.relativeTo[0];
        do {
            x += parent.offsetLeft;
            y += parent.offsetTop;
            offsetParent = parent.offsetParent || document.body;
            if (options.scroll) {
                do {
                    sl += parent.scrollLeft;
                    st += parent.scrollTop;
                    parent = parent.parentNode;
                } while (parent != offsetParent);
            }
            parent = offsetParent;
        } while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML' && parent != options.relativeTo);
        var returnValue = handleOffsetReturn(this[0], options, x, y, sl, st);
        if (returnObject) {
            $.extend(returnObject, returnValue);
            return this;
        } else {
            return returnValue;
        }
    },
    offsetParent: function() {
        if (!this[0]) error();
        var offsetParent = this[0].offsetParent;
        while (offsetParent && (offsetParent.tagName != 'BODY' && $.css(offsetParent, 'position') == 'static')) offsetParent = offsetParent.offsetParent;
        return $(offsetParent);
    }
});
var error = function() {
    throw "Dimensions: jQuery collection is empty";
};
var num = function(el, prop) {
    return parseInt($.css(el.jquery ? el[0] : el, prop)) || 0;
};
var handleOffsetReturn = function(elem, options, x, y, sl, st) {
    if (!options.margin) {
        x -= num(elem, 'marginLeft');
        y -= num(elem, 'marginTop');
    }
    if (options.border && (($.browser.safari && parseInt($.browser.version) < 520) || $.browser.opera)) {
        x += num(elem, 'borderLeftWidth');
        y += num(elem, 'borderTopWidth');
    } else if (!options.border && !(($.browser.safari && parseInt($.browser.version) < 520) || $.browser.opera)) {
        x -= num(elem, 'borderLeftWidth');
        y -= num(elem, 'borderTopWidth');
    }
    if (options.padding) {
        x += num(elem, 'paddingLeft');
        y += num(elem, 'paddingTop');
    }
    if (options.scroll && (!$.browser.opera || elem.offsetLeft != elem.scrollLeft && elem.offsetTop != elem.scrollLeft)) {
        sl -= elem.scrollLeft;
        st -= elem.scrollTop;
    }
    return options.scroll ? {
        top: y - st,
        left: x - sl,
        scrollTop: st,
        scrollLeft: sl
    } : {
        top: y,
        left: x
    };
};
var scrollbarWidth = 0;
var getScrollbarWidth = function() {
    if (!scrollbarWidth) {
        var testEl = $('<div>').css({
            width: 100,
            height: 100,
            overflow: 'auto',
            position: 'absolute',
            top: -1000,
            left: -1000
        }).appendTo('body');
        scrollbarWidth = 100 - testEl.append('<div>').find('div').css({
            width: '100%',
            height: 200
        }).width();
        testEl.remove();
    }
    return scrollbarWidth;
};
})(jQuery);

最佳答案

我认为您想要验证 this[0] 是否有值(value),或者它可能是空的。 尝试使用jquery提供的$.isEmptyObject 例如:

if($.isEmptyObject(this[0])){
error();
}

关于javascript - 是什么导致了这个 JS 控制台错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599739/

相关文章:

javascript - JQuery AJAX 尝试解析 XML,即使使用 "dataType": "JSON"

php - 如何在电子邮件中添加附件?

javascript - Masonry JS 忽略 CSS 转换

javascript - 智能手机文本输入 "done"的 JS 事件处理程序是什么

c# - jQgrid 搜索选项未按预期工作

javascript - 检测浏览器历史操作

javascript - d3.js 用异步函数替换 random() 数据函数

javascript - 在非表单中按下 "enter"时按钮隐藏且可点击

javascript - Jquery 单击另一个元素并显示选择选项

javascript - saveTable函数怎么写?