简体   繁体   English

如何计算内部有一些文本和固定宽度的div的高度

[英]how to calculate the height of a div with some text inside and a fixed width

I have the following code: 我有以下代码:

<div class="content">
                        <a href="/profile/Nat's Rare &amp; Raw" class="link-name"><strong>Irene Natalia</strong></a>
                        Hooray! we'll proceed with the shipping and inform the tracking # tomorrow :) Thank you, Angel! :)

                        <br>
                        <span class="date">7 days ago</span>
                    </div>

I wanted to know the height of this div from using javascript. 我想通过javascript知道这个div的高度。 Is there a way to do so? 有办法吗? This div has a fixed sized width (in this case 150px). 该div具有固定大小的宽度(在这种情况下为150px)。 I just wanted to know the height of the div say the text changed. 我只是想知道div的高度说文本改变了。 Preferred method is via jQuery but javascript is fine as well 首选方法是通过jQuery,但javascript也很好

EDIT: 编辑:

A very important note here is that this is a calculation before the div is being rendered. 这里一个非常重要的注意事项是,这是在渲染div之前的计算。 I am basically creating a pinterest layout and I need to calculate the height of the comments in the item card so that masonry can lay it out first before the image is loaded. 我基本上创建了一个pinterest布局,我需要计算项目卡中注释的高度,以便在加载图像之前,砌体可以先放置它。

Javascript: 使用Javascript:

element.offsetHeight;

This gives the height of an element 这给出了元素的高度

See about offsetHeight here and this is pure javascript. 在这里查看offsetHeight ,这是纯粹的javascript。

jQuery: jQuery的:

$elem.height(); - gives height of the element without padding , border and margin - 给出元素的高度,没有填充,边框和边距

$elem.innerHeight(); - gives height including padding - 给出包括填充在内的高度

$elem.outerHeight(true); - gives height including padding, border and margin - 给出高度,包括填充,边框和边距

EDIT: 编辑:

If you want to find the height of the element before it is rendered , then i can give you one of many possible solutions 如果要在渲染之前找到元素的高度,那么我可以为您提供许多可能的解决方案之一

  1. Append the element to the body with respective innerHTML's 使用相应的innerHTML将元素附加到正文
  2. Find the height using any of the above methods 使用上述任何方法查找高度
  3. Now,remove the element and of course , you found the height & element is not there 现在,删除元素,当然,你发现高度和元素不存在

See Fiddle : To get an element's rendered height 请参阅小提琴: 获取元素的渲染高度

jQuery jQuery的

var height = $('.content').height();
console.log(height);

Vanilla JS 香草JS

var height = document.getElementsByTagName('content').offsetHeight
console.log(height);

.height() 。高度()

Description: Get the current computed height. 描述:获取当前计算的高度。

在此输入图像描述


.innerHeight() .innerHeight()

Description: Get the current computed height, including padding but not border. 描述:获取当前计算的高度,包括填充但不包括边框。

在此输入图像描述


.outerHeight() .outerHeight()

Description: Get the current computed height, including padding, border, and optionally margin. 描述:获取当前计算的高度,包括填充,边框和可选的边距。

In jQuery: height will give the height without border or padding, innerHeight gives the height including padding but without border and outerHeight gives height with border and padding with the option to include margin as well. 在jQuery中: height将给出没有边框或填充的高度, innerHeight给出包括填充但不带边框的高度, outerHeight给出带边框和填充的高度以及包含边距的选项。

I vanillaJS, as Prasath K pointed out, offsetHeight will give you the height including padding and border. 我是vanillaJS,正如Prasath K指出的那样, offsetHeight会给你高度,包括填充和边框。

There isn't really a way to calculate height of an element that is not in the dom. 实际上没有办法计算不在dom中的元素的高度。 You can add it somewhere in the dom, calculate the height using one of the above methods and then remove it again. 您可以将它添加到dom中的某个位置,使用上述方法之一计算高度,然后再将其删除。

Try like . 试试吧。 height() like 高度()喜欢

var div_hght = $('.content').height();
alert(div_hght);

In addition to the techniques described above, in order to not have the element on screen when calculating, you can use visibility: hidden (CSS). 除了上述技术之外,为了在计算时不在屏幕上显示元素,您可以使用visibility: hidden (CSS)。 You can't usually use display: none , because the element is usually considered to have no dimensions in that state. 您通常不能使用display: none ,因为该元素通常被认为在该状态下没有维度。

You could also render to a div positioned below some obscuring element, using a negative z-index . 您还可以使用负z-index渲染到位于某个遮蔽元素下方的div。

Also, the answers here forget to mention the scrollHeight property, which can be used to get the full height of a DIV (all the scrollable content inside). 此外,这里的答案忘记提及scrollHeight属性,该属性可用于获取DIV的全部高度(内部的所有可滚动内容)。

To call it, you need to use an index like that: 要调用它,您需要使用这样的索引:

$('#content')[0].scrollHeight;

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

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