3

我有以下 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

有没有办法获得正确和一致的价值观?

4

1 回答 1

1

要获取任何元素的样式属性的确切值,您可以使用此

// 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' );

希望有帮助。

于 2013-06-07T06:55:35.867 回答