如何获得高度的百分比,而不是像素的内联样式? [英] How to get height in percentage instead of in pixels from inline style?
本文介绍了如何获得高度的百分比,而不是像素的内联样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以基本上,我有许多元素与 height
的内联样式百分比。虽然,当我尝试将高度保存为要使用的变量时,它会将其保存为像素。
So basically, I have many elements with an inline style of height
with percentages. Although, when I am trying to save the height as a variable to use, it is saving it as pixels.
例如:
<div class="wrapper">
<div style="height:10%;">Testing 123</div>
<div style="height:20%;">Testing 123</div>
<div style="height:30%;">Testing 123</div>
</div>
$('.wrapper > div').each(function () {
var height = $(this).css('height');
console.log(height);
});
上述代码将打印到控制台 40px
, 80px
和 120px
。我需要这些值作为百分比。有没有办法做到这一点,或者应该保存这些值在数据属性中使用?
The above code will print to the console 40px
, 80px
and 120px
. I need these values as percentages. Is there a way to do this, or should I save these values in a data attribute to use?
WORKING DEMO
推荐答案
使用:
$('.wrapper > div').each(function () {
var height = this.style.height;
console.log(height);
});
Updated Fiddle
这篇关于如何获得高度的百分比,而不是像素的内联样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文