通过 Javascript 设置使用 CSS 填充、边距和边框的 HTML 容器的绝对高度 (offsetHeight)
我想做一些类似设置 offsetHeight (offsetHeight 是只读属性)的操作 - 将 3 个 div (“d1”、“d2”、“d3”)放入一个容器(“c”)中:
<!DOCTYPE HTML>
<html>
<body>
<style type="text/css">
.c {
background-color:#FF0000;
overflow:hidden;
}
.d {
left:10px;
border:9px solid black;
padding:13px;
margin:7px;
background-color:#FFFF00;
}
</style>
<div class="c" id="c">
<div id="d1" class="d">text text text</div>
<div id="d2" class="d">text text text</div>
<div id="d3" class="d">text text text</div>
</div>
<script type='text/javascript'>
var h=600;
var hd = Math.floor(h/3);
var c = document.getElementById("c");
var d1 = document.getElementById("d1");
var d2 = document.getElementById("d2");
var d3 = document.getElementById("d3");
c.style.height=h +"px";
d1.style.height=hd +"px";
var hd2 = (2 * hd - d1.offsetHeight) +"px";
d1.style.height=hd2;
d2.style.height=hd2;
d3.style.height=hd2;
</script>
</body>
</html>
但是 - 首先:盒子不'不适合完美:-( 其次,样式很糟糕。你知道如何将 3 个 div (“d1”、“d2”、“d3”) 放入一个容器 (“c”) 中吗?
=> 也我不知道如何读取CSS属性“padding”和“margin”
alert(d1.style.paddingTop);
不起作用(也许因为它是由css-class定义的而不是直接的)
谢谢:-) 最好的问候托马斯
I want to do something like setting offsetHeight (offsetHeight is a read only property) - fit 3 div ("d1", "d2", "d3") into one container ("c"):
<!DOCTYPE HTML>
<html>
<body>
<style type="text/css">
.c {
background-color:#FF0000;
overflow:hidden;
}
.d {
left:10px;
border:9px solid black;
padding:13px;
margin:7px;
background-color:#FFFF00;
}
</style>
<div class="c" id="c">
<div id="d1" class="d">text text text</div>
<div id="d2" class="d">text text text</div>
<div id="d3" class="d">text text text</div>
</div>
<script type='text/javascript'>
var h=600;
var hd = Math.floor(h/3);
var c = document.getElementById("c");
var d1 = document.getElementById("d1");
var d2 = document.getElementById("d2");
var d3 = document.getElementById("d3");
c.style.height=h +"px";
d1.style.height=hd +"px";
var hd2 = (2 * hd - d1.offsetHeight) +"px";
d1.style.height=hd2;
d2.style.height=hd2;
d3.style.height=hd2;
</script>
</body>
</html>
but - first: the boxes doesn’t fit perfect :-( and secondly the style is bad. Do you have a idea how to fit the 3 div ("d1", "d2", "d3") into one container ("c")?
=> also I dont know how to read the css properties "padding" and "margin"
alert(d1.style.paddingTop);
doesn't work (maybe because it is defined by css-class and not direct)
Thank you :-)
Best regards Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的浏览器和您拥有的
DOCTYPE
决定了块元素的默认框模型。通常,默认值为content-box
,这意味着内边距、边框和边距都会添加到高度/宽度中,因此如果您有盒子,则需要将其纳入计算中模型为content-box
。另一种选择是,您可以使用
box-sizing
CSS 属性将框模型更改为border-box
。这意味着内边距和边框包含在高度和宽度中,并且只有边距添加到它们中。在我看来,这种盒子模型通常更方便做我想做的事情,所以我通常最终会切换。参考:
Which browser your using and what
DOCTYPE
you have determines the default box model for block elements. Usually, the default iscontent-box
, which means that the padding, border, and margin all add to the height/width, so you'll need to factor that into your calculations if you have the box model ascontent-box
.Another options is, you can change the box model to
border-box
using thebox-sizing
CSS property. This means that the padding and border are included in the height and width, and only the margin adds to them. In my opinion, this box model is usually a more convenient one for doing what I want, so I usually end up switching.Reference:
经过一些测试我找到了这个解决方案:
(适用于:Opera、Firefox 和 Google Chrome)
(box-sizing:使用 JavaScript 时在 Firefox 上不起作用?!)
After some testing I figure out this solution:
(works with: Opera, Firefox and Google Chrome)
(box-sizing: doesn't work on Firefox when used JavaScript?!)