我在 JavaScript 中创建了一个新的 div 元素,并附加了一些文本内容。现在我需要获取在 JavaScript 中创建的 div 的高度。
示例代码:
const str = 'One of the important elements of a web application is form processing and file uploads. PHP has excellent support for these kind of things which makes it so popular with developers.';
const container = document.createElement('div');
container.style.width = '1020px';
const textContent = document.createTextNode(str)
container.appendChild(textContent);
const width = container. ??? // Should be in number like container.clientHeight
const height = container. ???
文本内容将根据主题增长。如何在不加载 HTML DOM 的情况下获得虚拟 div 元素的高度和宽度。
注意:我没有在真正的 html 文档中添加 div。刚刚在 javascript 中创建,我正在将 div 转换为 PDF,为此我需要 div 的高度和宽度。
请帮助我如何做到这一点。