0

所以,这在我的脑海中似乎很容易做到,但我无法让它工作,谷歌也没有帮助我,你能看到我做错了什么吗?

我有 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>
4

2 回答 2

2

您不需要为此使用 javascript。在此处查看JSFiddle 演示

检查以下代码

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;}
于 2013-01-25T16:34:27.230 回答
1

所以你想让div你的屏幕和你的屏幕一样大吗? position: absolute; top: 0px; bottom: 0px; left: 0px; right: 0px;

于 2013-01-25T16:24:35.803 回答