gpt4 book ai didi

javascript - jquery $(window).height() 和 $(document).height() 返回相同

转载 作者:太空狗 更新时间:2023-10-29 12:27:06 25 4
gpt4 key购买 nike

我需要我的背景图片位于我的菜单 div 下方。因此,我没有将背景应用于 body 元素,而是放入了另一个包含我的 body div 的 bg div,并将宽度设置为 100%。(我的 body div 具有指定的宽度)我在 bg div 中设置了背景。

我测试了一下,因为文档不够长,得到了半张背景图。所以我正在尝试为此做一个 javascript 修复。

<!DOCTYPE HTML>
.....
<script type="text/javascript" src="jquery-min.js"></script>
<script type="text/javascript">
function setDocumentSize() {
alert($(window).height());
alert($(document).height());
if ($(window).height()>$(document).height()) {
var height = $(window).height()-$(document).height();
document.getElementById('bg').style.height=height+"px"
}
}
.....
</script>
.....
<body onload="setDocumentSize()">
<div class="menu">
.....
</div>
<div class="bg">
<div class="body">
.....(background in this div)
</div>
</div>

现在这两个警报都会弹出视口(viewport)高度。因此什么也没有发生。

我使用的是 Firefox 16.0.2

这是实际页面的链接 http://servapps.dyndns-work.com/abstract/

最佳答案

失败的另一个原因是文档类型声明 (<!DOCTYPE html>) 丢失。添加此修复 $(window).height()返回正确的视口(viewport)高度。

关于javascript - jquery $(window).height() 和 $(document).height() 返回相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271950/

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