gpt4 book ai didi

javascript - 如何捕获 DIV 的 css 属性的高度?例如 'height'

转载 作者:行者123 更新时间:2023-11-28 20:43:55 25 4
gpt4 key购买 nike

如何捕获 DIV 的 css 属性的高度?例如“高度”。这是 HTML。我正在使用jquery插件高度

<!doctype html>  
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Herald</title>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.1.min.js'> </script>
<script type='text/javascript' src='http://orlandovisitornetwork.com/wp- includes/js/jquery/jquery.js?ver=1.6.1'></script>
<script type="text/javascript"></script>

</head>


<body>

<div id="main" class='test'>
This is just a test
</div>

<script type="text/javascript">
window.onload=function()
{
var height = $("test").height();

if (height > 0px)
{
alert("DIV Height is" + 'height');
}
};
</script>
</body>
</html>

这是CSS。当然,这是非常简化的版本。

.test {
height: 500px;
}

最佳答案

这就是window.getCompulatedStyle 的用途。对于您的情况:

var height = window.getComputedStyle(document.getElementById("main")).height

(ID比类名更可靠。如果只有一个类名,则不应使用类名)

关于javascript - 如何捕获 DIV 的 css 属性的高度?例如 'height',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13809569/

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