动态地将元素的高度更改为另一个

时间:2011-07-01 17:55:40

标签: javascript jquery html css

Column A and B
在这里,我们有两列A和B.事情是列A通常包含更多内容,因此其高度更高(在这种情况下为126px),列B具有更少的内容,因此它保持更短(此处为94px)。现在我想确定B = A的高度,考虑到A列的高度可能会由AJAX动态改变,但为了与A列保持同步,B列的高度也必须改变。
<div id="A">filer text</div> | <div id="B">filler text2</div> 现在可以通过使用jQuery或一些js来获取id #A的元素高度并将其设置为#B,但问题在于内容动态变化。

2 个答案:

答案 0 :(得分:6)

$("#a").css("height", $("#b").css("height") );

然后可以将其放入回调函数中,比如说:

$.ajax({
  ...
  success:function(msg){
    // could be optimized by storing off of the comparision
    if( $("#a").height() > $("#b").height() ){
      $("#b").css("height", $("#a").css("height") );
    }
  }
});

答案 1 :(得分:0)

没有JQuery:

var d = document;
d.getElementById("B").style.height = d.getElementById("A").offsetHeight;
相关问题