假设我们有一个表单加载到另一个站点的iframe中。现在它有一个新版本,并且必须匹配与前一个表单相同的高度,以便完全适合加载它的站点。
即使使用JavaScript,您如何检查网页的高度是多少?然后我可以检查两者并进行比较。就像页面的垂直像素数一样。
修改
忽略iframe,只查找当前html文档的高度,就是这样。
答案 0 :(得分:4)
可以使用以下方法获取文档的高度:
$(document).height()
function getDocHeight() {
var D = document;
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}
对于 Max(documentHeight,browserHeight),请使用:
Math.max($(document).height(), $(browser).height());
function getDocHeight() {
var D = document;
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}
答案 1 :(得分:1)
从你的问题中不清楚你究竟在问什么。但是如果你想要获得父文档的高度,那么你需要使用:
var parents_height = parent.document.body.clientHeight;
这将返回父窗口文档的高度。但是,如果iframe / popup位于同一个域中,它将仅返回它。
如果您只对当前文档的高度感兴趣,请使用:
var height = document.body.clientHeight;
但是,如果您在CSS中操纵了body标签的高度,那么您可能会遇到问题。最好像使用jQuery一样:
$(document).height();
答案 2 :(得分:0)
试试这个
var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
答案 3 :(得分:0)
scrollHeight属性怎么样
https://developer.mozilla.org/en-US/docs/Web/API/element.scrollHeight
答案 4 :(得分:-1)
您可以使用jQueries高度函数:
$(document).height();
答案 5 :(得分:-1)
这个简单的javascript代码行将为您提供答案
document.body.offsetHeight;
请记住,您需要在加载整个文档后获取它
<script>
window.onload = function() {
document.body.offsetHeight;
}
</script>