gpt4 book ai didi

javascript - 将某个类的div设置为与窗口innerHeight相同的高度

转载 作者:行者123 更新时间:2023-11-28 20:40:22 24 4
gpt4 key购买 nike

所以,这在我看来很容易做到,但我无法让它发挥作用,而且 Google 也没有帮助我,你能看出我做错了什么吗?

我有 5 个 div,我希望它们与浏览器窗口的高度相同。即使调整大小。

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function setDivHeight(){
var windowHeight = window.innerHeight;
if(windowHeight > 400){
document.getElementsByClassName("minHeight").style.backgroundColor = 'black';
}
else if(windowHeight < 400){
document.getElementsByClassName("minHeight").style.backgroundColor = 'red';
}
}

window.addEventListener("resize",setDivHeight,false);
</script>
</head>
<style type="text/css">

body{margin:0; padding:0;}

.minHeight{
height:20px;
}

.a{
background: #09C;
}
.b{
background: #09b;
}
.c{
background: #09d;
}
.d{
background: #09e;
}
.e{
background: #09f;
}



</style>
<body onload="setDivHeight()">
<div class="a minHeight"></div>
<div class="b minHeight"></div>
<div class="c minHeight"></div>
<div class="d minHeight"></div>
<div class="e minHeight"></div>
</body>
</html>

最佳答案

您不需要为此使用 JavaScript。检查JSFiddle DEMO HERE

检查以下代码

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div class="a minHeight"></div>
<div class="b minHeight"></div>
<div class="c minHeight"></div>
<div class="d minHeight"></div>
<div class="e minHeight"></div>
</body>
</html>

CSS

html{height:100%}
body{margin:0; padding:0;height:100%;}
.minHeight{
height:100%;
width: 40px;
}
.a{
background-color: #09C;
}
.b{
background-color: #09b;
}
.c{
background-color: #09d;
}
.d{
background-color: #09e;
}
.e{
background-color: #09f;
}

div{float: left;}

关于javascript - 将某个类的div设置为与窗口innerHeight相同的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14525807/

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