外部元素利润率不等于内部元件保证金 [英] Outer element margin not equal to inner element margin

查看:144
本文介绍了外部元素利润率不等于内部元件保证金的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的WebView来显示我的应用程序的HTML内容。我需要动态地发现,包括填充,边距和边框元素(通常是div)的最大高度。我使用的JavaScript,而不能使用jQuery。

I am using an Android WebView to display my app's HTML content. I need to dynamically find the full height of an element (usually a div) including padding, margin, and border. I am using JavaScript, and cannot use jQuery.

我一直使用 scrollHeight属性在我的div来获得高度。我使用 VAR风格= window.getComputedStyle(DIV,空)的div计算样式; ,并通过调用风格得到了上下页边距.marginTop style.marginBottom 。他们都是0像素。问题是, scrollHeight属性仍然不占属于div的边缘的高度。

I have been using scrollHeight on my div to get the height. I got the computed style of the div using var style = window.getComputedStyle(DIV, null); and got the top and bottom margin by calling style.marginTop and style.marginBottom. They were both 0px. The problem is that scrollHeight still does not account for the height of the margins belonging to div's.

我得到了我的div(通常是P或H *标记)的子元素,并取得相同的字段相同的方式: scrollHeight属性 marginTop marginBottom 。在 scrollHeight属性是完全一样的(因为我通常只有每格一个孩子),但通常是在孩子未反映在顶部或底部边距值父元素。

I got the child elements of my div (usually a P or H* tag) and obtained the same fields the same way: scrollHeight, marginTop, and marginBottom. The scrollHeights are the exact same (as I usually only have one child per div), but there is usually a top or bottom margin value in the child that is not reflected in the parent element.

我真的不明白为什么会这样。看来通过孩子的利润率呈现母公司之外,但父母有没有它的知识,并不能直接访问它。

I don't really understand why this is so. It appears as through the child's margin is rendered outside of parent, but the parent has no knowledge of it, and cannot directly access it.

我要寻找一种方法来访问通过父元素被孩子所定义的余量。这可能吗?

<一个href=\"http://stackoverflow.com/questions/2555002/how-to-get-the-height-of-a-div-considering-inner-elements-margins\">This问是非常有益的,但​​我没有找到为什么它是呈现这样的,线程似乎已冷的根本原因。

This Question was very helpful, but I did not find an underlying reason for why it is rendered this way and the thread seems to have grown cold.

谢谢!

推荐答案

森达维达斯我指出了正确的方向,这样的感谢!

Sime Vidas pointed me in the right direction, so thanks!

我发现自己试图找出的div与子女之间的崩溃边缘的准确高度(但在我的问题的措辞多少不同)。当访问信息线槽DOM它会告诉我的父母和孩子的样式属性,但它并没有报告任何高度崩溃边缘。

I found myself trying to find out the exact height of the collapsed margins between divs and their children (but worded it much differently in my question). When accessing that information trough the DOM it will tell me style attributes of the parents and children, but it does not report the height of any collapsed margins.

所以...真正的答案我的问题是NO。我一直没能找到一种方法来直接访问崩溃边缘的属性。

So...the real answer to my question is NO. I have not been able to find a way to directly access the attribute of the collapsed margins.

什么是一样好,正在我的div的的offsetTop 属性,找到之间的差异的offsetTop 属性接下来的DIV。这使总高度,包括利润的最终渲染。要找到边缘,减去 scrollHeight属性

What is just as good is taking the offsetTop attribute of my div, and finding the difference between the offsetTop attribute of the next Div. That gives the total height including final rendering of margins. To find the margins only, subtract the scrollHeight.

这可能并不适用于所有的场景,但绰绰有余我的目的。

This may not work in all scenarios, but was more than enough for my purposes.

这篇关于外部元素利润率不等于内部元件保证金的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆