2

嘿伙计们希望你们能帮助我

我有一个奇怪的问题。我正在处理一个有两个浮动 div(以及许多其他内容)的网页。为了使浮动 div 的高度相等,我添加了以下代码

$(document).ready(function(){

   alert($('#body-left').height());
    if($('#body-left').height()>$('#sidebar').height()){
     $('#sidebar').height($('#body-left').height());

    }
    else{

    $('#body-left').height($('#sidebar').height());

    }

});

请参阅“alert($('#body-left').height());”行,当我删除该行时,它停止工作:/。IE :

这是我不添加 alert() 行时发生的情况: 在此处输入图像描述

这就是我添加警告行时发生的情况

在此处输入图像描述

任何想法为什么会这样?:/


抱歉,回复晚了,但我得到了它的使用

if($('#body-left').height()>$('#sidebar').height()){
    setTimeout(function(){
     $('#sidebar').height($('#body-left').height());
    },100);

}
else{
    setTimeout(function(){
    $('#body-left').height($('#sidebar').height());
    },100);
}

有任何想法吗?

4

2 回答 2

1

如果你想要相等的高度......试试这个插件:

$.fn.setMinHeight = function(setCount) {
  for(var i = 0; i < this.length; i+=setCount) {
    var curSet = this.slice(i, i+setCount), 
        height = 0;
    curSet.each(function() { height = Math.max(height, $(this).height()); })
          .css('height', height);
  }
  return this;
};

将上面的代码复制并粘贴到一个 JS 文件中并链接到它。然后这样称呼它...

$(".theItems").setMinHeight(2); // Pass in number of items to include.

希望这可以帮助。

迈克尔。

于 2012-07-12T17:07:10.570 回答
1

这个问题为基础,您可以快速获取任意数量元素的最大高度。如果你给他们所有的课程equalheight,你可以这样做:

$(document).ready(function() {
    var maxheight = $.map($('.equalheight'), function(val, i) {
        return $(val).height();
    }).max();
    $('.equalheight').height(maxheight);
});

Array.prototype.max = function() {
    return Math.max.apply(Math, this);
};

http://jsfiddle.net/mblase75/NR9TZ/

于 2012-07-12T18:27:16.120 回答