如何知道元素的宽度是绝对的还是相对的?

时间:2022-03-24 13:12:50

I'm coding a javascript scriptlet on which I'm resizing a DOM element. But I need to know if the element was originally sized absolutely or relatively.

我正在编写一个javascript scriptlet,我正在调整DOM元素的大小。但是我需要知道该元素的原始大小是绝对的还是相对的。

Of corse I can read its clientWidth property, but it always returns an absolute integer. How can I know if it was originally specified by a percentage number in a CSS? Is there any CSS property -readable from javascript- containing the original width expression?

我可以读取它的clientWidth属性,但它总是返回一个绝对整数。如何知道它是否最初由CSS中的百分比数字指定?是否有任何CSS属性 - 从javascript-可读 - 包含原始宽度表达式?

1 个解决方案

#1


1  

If your width value is inside the tag you can use document.getElementById("id").style.width. But if your css is inside a tag style, that's going to make you use jquery for a quick solution or you are going to have to create your own function to extract this value, like this one: How to get an HTML element's style values in javascript?

如果您的宽度值在标记内,则可以使用document.getElementById(“id”)。style.width。但是如果你的css在标签样式中,那么你将使用jquery来快速解决问题,或者你将不得不创建自己的函数来提取这个值,如下所示:如何获取HTML元素的样式值JavaScript的?

#1


1  

If your width value is inside the tag you can use document.getElementById("id").style.width. But if your css is inside a tag style, that's going to make you use jquery for a quick solution or you are going to have to create your own function to extract this value, like this one: How to get an HTML element's style values in javascript?

如果您的宽度值在标记内,则可以使用document.getElementById(“id”)。style.width。但是如果你的css在标签样式中,那么你将使用jquery来快速解决问题,或者你将不得不创建自己的函数来提取这个值,如下所示:如何获取HTML元素的样式值JavaScript的?