【问题标题】:Android WebView: Element height returned by JavaScript is inconsistentAndroid WebView:JavaScript返回的元素高度不一致
【发布时间】:2013-06-07 06:13:08
【问题描述】:

我有以下 HTML 文档:

<html>
    <head>
    </head>
    <body>
        <div>
            <p>blah blah blah</p>
        </div>
    </body>
</html>

我需要确定&lt;div&gt;&lt;/div&gt; 的高度以相应地调整我的WebView 高度,我执行以下方式:

document.getElementsByTagName('div')[0].scrollHeight

有时返回的值是正确的,有时它比实际值要少一些。我在WebViewClient.onPageFinished()方法中调用了这段JavaScript,所以此时页面应该已经渲染好了。

我也试过.clientHeight.offsetHeight,甚至.getBoundingClientRect().height

有什么方法可以得到正确一致的值?

【问题讨论】:

    标签: javascript android dom android-webview


    【解决方案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' );
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多