javascript - 用于检查页面高度的 jquery 代码似乎无法正常工作

标签 javascript jquery html css dom

我这里有一小段 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/

相关文章:

javascript - GLatLngBounds - 错误的中心和缩放级别

html - 强制 div 到下一列

JavaScript 不替换所有元素

Jquery - 如果 'draggable div' 的大小大于 'droppable div',则不会发生 Drop 事件

javascript - 如何在没有 jQuery 的情况下使用 JavaScript 遍历每个带有类名的选中复选框

javascript - ElectronJS - 从远程服务器缓存 HTML 和 JS 文件

html - 是什么让 <button> 元素上的文本垂直居中?

javascript - 数据未显示在选择标签中

javascript - Grails - 在 remoteSubmit 中使用 javascript 调用渲染 div

javascript - 收到错误 - 如果对上面的列使用非字符串访问器,则需要列 ID - React