javascript 如何使用javascript获取元素的高度,包括填充和边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4910054/
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-25 15:20:34  来源:igfitidea点击:

how to get the height of a element including padding and borders using javascript

javascripthtmlcss

提问by harry

i would like to know how to get the height of a element including padding and borders using javascript so ill know the actually height the element has when rendered in a browser. Can anyone help me please and im still new to javascript

我想知道如何使用 javascript 获取元素的高度,包括填充和边框,所以我不知道元素在浏览器中呈现时的实际高度。谁能帮帮我,我还是 javascript 新手

回答by Matt Ball

You're looking for element.offsetHeightand element.offsetWidth.

您正在寻找element.offsetHeightelement.offsetWidth

According to quirksmode.com, these properties work in all of the major browsers. Hooray!

根据 quirksmode.com,这些属性适用于所有主要浏览器。万岁!

回答by aztack

Using jQuery, you can get element height including padding with $(ele).outerHeight(), get element height including padding, border and margin by $(ele).outerHeight(true).

使用 jQuery,您可以获取包含 padding 的元素高度 $(ele).outerHeight(),获取包含 padding、border 和 margin by 的元素高度 $(ele).outerHeight(true)

see also https://api.jquery.com/outerHeight/

另见https://api.jquery.com/outerHeight/