提问者:小点点

如何使用jQuery设置DIV元素的高度


我想实现以下目标

1)单击.step2-opt

2).quiz__drawer div应获取.step2info的高度

3)HTML输出应为

<div class="quiz__drawer" style="height:height of .step2info div">

4)使用以下jQuery代码

$('body').on("click", '.step2-opt', function () {
  $(".quiz__drawer").height(function(){
    $(".step2info").outerHeight();
  });
});

请帮助我修复jquery代码。


共2个答案

匿名用户

这里有一个例子供你尝试,希望能有所帮助

null

$('body').on("click", '.step2-opt', function () {
  // get the height of step2info
  var height = $('.step2info').css("height");
  // set the height of quiz__drawer
  $('.quiz__drawer').css("height", height)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="step2info" style="height: 200px; width: 200px; background-color: red;"></div>
<div class="quiz__drawer" style="height: 100px; width: 200px; background-color: green;"></div>
<button class="step2-opt">set height</button>

匿名用户

如您所见,在单击first div:

null

$.fn.equalizeHeights = function(){
  return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
}

function test(){

$('.step2info, .quiz__drawer').equalizeHeights();

}
<script
  src="https://code.jquery.com/jquery-3.5.1.js"
  integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
  crossorigin="anonymous"></script>

<body>
<div class="step2info" onclick="test()">dkdpodkds<br>soskdsopkdspodskpskpkpk<br>sopkspokdspskposkspksapk<br></div>
<div class="quiz__drawer"></div>
</body>