如何添加下面的代码来确定 DIV 是否有任何 margin-top/margin-bottom?我目前的情况是,我有 2 列(DIV),其中 1 列有顶部边距,另一列没有,这会导致列无法正确对齐。
抱歉,代码已被缩小,我丢失了原始代码。
$.fn.equalHeight = function (a) {
var b = {
delay: 100,
minusHeight: 0
};
var a = $.extend(b, a);
var c = 0;
var d = 0;
var e = $(this);
setTimeout(function () {
e.each(function () {
c = $(this).outerHeight();
d = c > d ? c : d
});
return e.each(function () {
var b = $(this);
var c = d - (b.outerHeight() - b.height()) - a.minusHeight;
var e = jQuery.browser.msie && jQuery.browser.version < 7 ? "height" : "min-height";
b.css(e, c + "px")
})
}, a.delay)
};
最佳答案
您应该将 true
作为参数添加到 .outerHeight()
以便在计算中包含边距。
但是您的代码还有另一个问题。您无法因超时而返回任何内容,并且您似乎已将返回代码放在那里。
这意味着您正在破坏 jQuery 链接系统..
你的代码可能应该是
$.fn.equalHeight = function(a) {
var b = {
delay: 100,
minusHeight: 0
};
var a = $.extend(b, a);
var c = 0;
var d = 0;
this.each(function() {
c = $(this).outerHeight(true);
d = c > d ? c : d;
});
return this.each(function() {
var b = $(this);
var c = d - (b.outerHeight(true) - b.height()) - a.minusHeight;
var e = (jQuery.browser.msie && jQuery.browser.version < 7) ? "height" : "min-height";
setTimeout( function(){
b.css(e, c + "px");
}, a.delay);
});
};
关于jquery等高插件添加边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7877625/