简体   繁体   English

计算HTML内容的高度和宽度

[英]Calculate height and width of HTML content

I am creating a generic myWindow extending Ext.window.Window component. 我正在创建一个通用的myWindow扩展Ext.window.Window组件。 here I need to support one feature to auto adjust height and widht of window. 在这里,我需要支持一种功能来自动调整窗口的高度和宽度。 This height and width will depend on height/width of HTML content inside the Window. 该高度和宽度将取决于Window内HTML内容的高度/宽度。 Is there any way in EXT JS or javascrip that can provide me height and width of html content. EXT JS或javascrip中有什么方法可以为我提供html内容的高度和宽度。

To get the height or width of an element in pixels (including padding and borders), use the offsetHeight and offsetWidth properties of the object. 要获取以像素为单位的元素的高度或宽度(包括填充和边框),请使用对象的offsetHeightoffsetWidth属性。 These values are readonly. 这些值是只读的。

If you have set the style of the element, you can get the height and width (excluding padding and border) with the style property: 如果设置了元素的样式,则可以使用style属性获得高度和宽度(不包括填充和边框):

var el = document.getElementById('elementId');
var width = el.style.width;
var height = el.style.height;

You can also set the height or width of the element this way. 您也可以通过这种方式设置元素的高度或宽度。

el.style.width = 40 + 'px';

'px' can be changed to any valid unit, eg em. 可以将'px'更改为任何有效单位,例如em。

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

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