简体   繁体   English

如何从纯JavaScript获取div的最长高度?

[英]How to get the longest height of the div from pure javascript?

I'm trying to achieve what can be achieved via jQuery but I'm avoiding jQuery for learning javascript. 我正在尝试通过jQuery实现什么,但是我避免使用jQuery学习JavaScript。 I'm trying to get the highest number from .map . 我正在尝试从.map获取最大的数字。 This is what I've done so far. 到目前为止,这是我所做的。

function equalHeight(className) {
  var i = Array.from(document.getElementsByClassName(className));
  i.forEach(function(items) {
    console.info(Math.max(items.scrollHeight));
  });
}

but this logs nothing 但这什么都不记录

console.info(items.scrollHeight);

logs all three numbers. 记录所有三个数字。

I know I'm missing an elementary mistake, Can someone point out what I'm doing wrong? 我知道我错过了一个基本的错误,有人可以指出我在做什么错吗?

Basically, I want to get the longest height and set it to the rest of the divs 基本上,我想获得最长的高度并将其设置为其他div

I would use .getBoundingClientRect() and just compare for every iteration 我将使用.getBoundingClientRect()并在每次迭代中进行比较

function equalHeight(className) {
  const elements = Array.from(document.getElementsByClassName(className));
  let highest = 0;

  elements.forEach(function(item) {
     const itemH = item.getBoundingClientRect().height;
     highest = itemH > highest ? itemH : highest;
  });

  return highest;
}

Docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect 文件: https//developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

I am not 100% sure exactly what you are after, but a few things: 我不是100%确切地知道您要追求的是什么,但是有几件事:

1) Switching from getElementsByClassName to querySelectorAll gives you an object that has forEach built in. 1)从getElementsByClassName切换到querySelectorAll将为您提供一个内置forEach的对象。

2) You were passing in only 1 value to Math.max 2)您只向Math.max传递了1个值

 function equalHeight(className) { var max = 0; document.querySelectorAll(className).forEach( function(el) { console.info(Math.max(el.scrollHeight, max)); if (el.scrollHeight > max) { max = el.scrollHeight; } } ); return max; } var max = equalHeight('.test'); console.info('Max div height:', max); 
 div { border: 1px dashed red; margin: 2px 0; } 
 <div class="test">one</div> <div class="test">one<br/>two</div> <div class="test">one<br/>two<br/>three</div> 

UPDATE UPDATE

To your question "now how do I use this value as height for the rest of the divs": 对于您的问题“现在如何使用此值作为其余div的高度”:

 function getMaxHeight(className) { var max = 0; document.querySelectorAll(className).forEach( function(el) { console.info(Math.max(el.scrollHeight, max)); if (el.scrollHeight > max) { max = el.scrollHeight; } } ); return max; } function setHeight(className, height) { document.querySelectorAll(className).forEach( function(el) { el.style.height = height+'px'; } ); } var max = getMaxHeight('.test'); console.info('Max div height:', max); setHeight('.test', max); 
 div { border: 1px dashed red; margin: 2px 0; } 
 <div class="test">one</div> <div class="test">one<br/>two</div> <div class="test">one<br/>two<br/>three</div> 

But, once you do this then none of the DIVs will change height again. 但是,一旦执行此操作,所有DIV都不会再次更改高度。

You could set el.style.minHeight and then, check again if you change anything in the DIVs. 您可以设置el.style.minHeight ,然后再次检查是否更改了DIV中的任何内容。

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

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