对于某些人来说,这可能是一个非常愚蠢的问题,但我肯定无法理解这背后的问题。代码相当简单,如下所示:
HTML:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>####</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body onload="resizeDiv();">
<div id="testElement">ABCD</div>
<div id="secElement">ABC</div>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>
CSS:
html , body{
height:100%;
}
#testElement{
background-color:black;
color:white;
}
#secElement{
font-size:50px;
}
JS:
(function immediate(){
document.getElementById('secElement').textContent = window.innerHeight;
}());
function resizeDiv(){
document.getElementById('testElement').style.height = (window.innerHeight - 50) * 0.9;
}
现在,通过这段代码,div
与 id
'testElement' 的高度应为 window.innerHeight - 20
的 90% .但是,似乎没有任何效果。请帮忙。
最佳答案
添加“px”:
document.getElementById('testElement').style.height = (window.innerHeight - 50) * 0.9 + "px";
关于javascript - 设置相对于 `window.innerHeight` 的高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454073/