简体   繁体   English

如何使用jQuery查找DIV的渲染高度?

[英]How to find the rendered height of a DIV with jQuery?

I am attempting to find the rendered height of two div. 我试图找到两个div的渲染高度。 They have the same class (.defense) but are within separate containers (side-1 and side-2). 它们具有相同的类(.defense),但位于单独的容器(side-1和side-2)中。 I need to use these heights, as the height is set to auto in the CSS, to use in my jQuery to decide which should the be the height of both divs when loaded. 我需要使用这些高度,因为在CSS中将高度设置为auto,以便在我的jQuery中使用哪个高度来确定加载时两个div的高度。

Here is my jQuery code: $(document).ready(function() { $(window).load(function() { 这是我的jQuery代码:$(document).ready(function(){$(window).load(function(){

      var sideOneDefense = $('.side-1 .defense').height();
      var sideTwoDefense = $('.side-2 .defense').height();

      if (sideOneDefense >= sideTwoDefense) {
        $('.defense').css("height",sideOneDefense)
      }
      else {
        $('.defense').css("height",sideTwoDefense)
      }
    });
});

You are using the code on load by the way after dom is ready the value is not what you expect. 您准备好在dom准备好后使用加载的代码,该值不是您期望的值。 Use the code inside ready handler instead: 请改用就绪处理程序中的代码:

$(document).ready(function() {

  var sideOneDefense = $('.side-1 .defense').height();
  var sideTwoDefense = $('.side-2 .defense').height();

  if (sideOneDefense >= sideTwoDefense) {
    $('.defense').css("height",sideOneDefense)
  }
  else {
    $('.defense').css("height",sideTwoDefense)
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM