我有以下 HTML 文档:
<html>
<head>
</head>
<body>
<div>
<p>blah blah blah</p>
</div>
</body>
</html>
我需要确定 <div></div>
的高度调整我的 WebView
相应的高度,我按以下方式执行:
document.getElementsByTagName('div')[0].scrollHeight
有时返回的值是正确的,有时会比实际值小一点。我将这段 JavaScript 称为 WebViewClient.onPageFinished()
方法,所以此时页面应该已经呈现。
我也试过.clientHeight
, .offsetHeight
,甚至 .getBoundingClientRect().height
有什么方法可以得到正确一致的值吗?
最佳答案
要获取任何元素的样式属性的准确值,您可以使用此
// get the reference to the element
var myDiv = document.getElementsByTagName('div')[0];
// get the desired height attribute
var computedHeight = document.defaultView.getComputedStyle( myDiv, null ).getPropertyValue( 'height' );
希望对您有所帮助。
关于javascript - 安卓 WebView : Element height returned by JavaScript is inconsistent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16977429/