我这里有一小段 jqery 代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
hasVBar="";
hasHBar="";
$(document).ready(function() {
// Check if body height is higher than window height :)
if ($(document).height() > $(window).height()) {
// alert("Vertical Scrollbar! D:");
hasVBar="y";
}
// Check if body width is higher than window width :)
if ($(document).width() > $(window).width()) {
//alert("Horizontal Scrollbar! D:<");
hasHBar="n";
}
});
</script>
现在在我的 html 页面中,第一个 div 的高度是 200px,下面是另一个 div,每个 div 在不同的 div 中包含 3 个 paras。我通过 javascript onclick 显示隐藏这 3 个参数。问题是即使这 3 个 div 处于隐藏状态(显示:无和可见性:隐藏),垂直滚动条也会出现。我期望只有当这 3 个 div 的任意组合足以使文档的高度高于窗口高度时,垂直滚动条才会弹出。至少最初是在页面打开时(然后所有 3 个 div 都处于隐藏状态)。
我希望我已经能够清楚地表达我的疑问。 有什么建议 ??? 谢谢..提前...
window.onload=function starterJobs(){
document.getElementById('abtpri').style.visibility='hidden';
document.getElementById('abtpri').style.display='none';
}
<script type="text/javascript">
function hidePara(whichId) {
showhide = document.getElementById(whichId).style.display;
if (showhide=='block') {
document.getElementById(whichId).style.display='none';
document.getElementById(whichId).style.visibility='hidden';
} else {
document.getElementById(whichId).style.display='block';
document.getElementById(whichId).style.visibility='visible';
}
}
</script>
<div style=" position:relative;background:none; width:auto; height:25px; padding:10px 0px 10px 0px;">
<div style="position:relative; top:0px; left:0px; background:transparent url(lftarw.gif) no-repeat; width:25px; height:25px; cursor:pointer" onclick="hidePara('abtpri'); "></div>
<div id="printing1" class="h1type4" style="position:relative; top:-25px; left:25px; background-color:none; width:auto; height:20px; padding-top:5px; margin-right:25px;">Printing</div>
</div>
<div id="abtpri" style="padding-left:25px;">
ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
</div>
最佳答案
那是因为您为每个 div 定义了一个高度。 因此,即使它们不可见,它们仍应占用该空间。
没有高度,div 应该在可见时展开以适应您的内容,在隐藏时消失。
关于javascript - 用于检查页面高度的 jquery 代码似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766651/