有一个 HTML 报告,我将样式应用为 “overflow:auto” 数据动态填充,因此我需要检测是否存在垂直滚动条报告中存在或不存在。
在研究了许多论坛后,所有论坛都建议比较 scrollHeight
和 clientHeight
,我尝试了以下方法:
$( document ).ready(function() {
var myReport = document.getElementById("report_id");
if (myReport.scrollHeight > myReport.clientHeight) {
alert("has a scrollbar");
} else {
alert("has NO scrollbar");
}
});
这不起作用。 结果始终为真,即“有滚动条”,即使没有垂直滚动条。
请提出建议。
P.S:以上代码仅用于测试目的。
最佳答案
<script>
jQuery(document).ready(function(){
var outerHeight = $("#report_id").outerHeight();
var scrollHeight = $('#report_id')[0].scrollHeight
if (outerHeight < scrollHeight) {
alert("has a scrollbar");
}
else
{
alert("has NO scrollbar");
}
});
</script>
关于javascript - 使用 JQuery/Javascript 检测垂直滚动条是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888068/