gpt4 book ai didi

javascript - 如何从内联样式中获取百分比而不是像素的高度?

转载 作者:搜寻专家 更新时间:2023-10-31 22:29:25 25 4
gpt4 key购买 nike

所以基本上,我有很多元素都带有 height 的内联样式和百分比。虽然,当我尝试将高度保存为要使用的变量时,它会将其保存为像素。

例如:

<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);
});

以上代码将打印到控制台 40px80px120px。我需要这些值作为百分比。有没有办法做到这一点,或者我应该将这些值保存在数据属性中以供使用?

WORKING DEMO

最佳答案

您可以使用:

$('.wrapper > div').each(function () {
var height = this.style.height;
console.log(height);
});

Updated Fiddle

关于javascript - 如何从内联样式中获取百分比而不是像素的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23235728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com