javascript 获取 <DIV> 的高度 - 返回 'undefined'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13441160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:47:18  来源:igfitidea点击:

Getting the height of a <DIV> - returns 'undefined'

javascripthtml

提问by labowhat

I have a problem getting the height of a (the one in the code below, with class="post_div"). And I want to return with a document.write whitin the HTML body - maybe later using it as height for other elements, that I want to be equal. I have tried every possible code for getting the height of such an element, but every time it returns 'undefined'. I have also tried setting height=auto for the DIV. I am almost sure my problem not has to do with the way I get the height, but have no other idea for what else it could be! Therefore I have choosen to bother you with the full structure of my coding. The JavaScript is placed in a separate document containing only the following statement:

我在获取 a 的高度时遇到问题(下面代码中的高度为 class="post_div")。我想返回一个 document.write whitin HTML body - 也许以后将它用作其他元素的高度,我希望它是平等的。我已经尝试了所有可能的代码来获取此类元素的高度,但每次都返回“未定义”。我也试过为 DIV 设置 height=auto。我几乎可以肯定我的问题与我获得高度的方式无关,但不知道还有什么可能!因此,我选择用我的编码的完整结构来打扰您。JavaScript 被放置在一个单独的文档中,仅包含以下语句:

var result=document.getElementById('postHeight').offsetHeight;

My HTML document looks like this

我的 HTML 文档看起来像这样

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<head>
<title>...</title>
<link rel="stylesheet" type="text/css" media="all" href="C:\Users\Lasse\Desktop\hg2\hg2.css" />
<script src="C:\Users\Lasse\Desktop\hg2\hg2.js"></script>
</head>
<body>

<script>document.write(result)</script>

<div id="postHeight" class="post_div">
...             
</div>


</body>
</html>

Hope you can help! Thanks!

希望你能帮上忙!谢谢!

回答by Niet the Dark Absol

There is no element with the ID postHeight. Perhaps you meant

没有带有 ID 的元素postHeight。也许你的意思是

var result=document.getElementById('content').offsetHeight;

Additionally, this calculation must be done afterthe element you are calculating has been loaded (so either by putting the <script>after the element, or deferring execution with onloador similar).

此外,这种计算方法,要做到你计算已经加载的元素(通过将这样无论是<script>在元素后,或推迟执行与onload或类似)。

回答by Dennis

Since your script tag occurs before the element exists in the DOM, you can't get the height. You could put an event handler waiting for the document to load before getting the height or move that script to the bottom of the page, but your document.writewould still be called before the element existed. You need to rethink your design.

由于您的脚本标记出现在元素存在于 DOM 之前,因此您无法获取高度。您可以在获取高度之前放置一个事件处理程序等待文档加载或将该脚本移动到页面底部,但您document.write仍然会在元素存在之前被调用。你需要重新考虑你的设计。

Sample code:

示例代码:

<div id=getme>
    some text to give it height.
</div>
<script>
    document.write(document.getElementById("getme").clientHeight);
</script>

回答by AmShaegar

To get the heightof your content<div>you just need the following code:

要获得height你的,content<div>你只需要以下代码:

var result = document.getElementById('content').style.height;

See an example here: http://jsfiddle.net/cayRP/

在此处查看示例:http: //jsfiddle.net/cayRP/