我想在一些特定的像素之后在另一个div中追加一个div。例如,首先,我创建了一个Div而不是用offsetHeight
来计算它的高度。现在我想在中间追加一个Div Inside父Div(父亲div高度的一半)
var ts = document.querySelectorAll('article')[0];
var ks = ts.offsetHeight;
var lirf = ks/2;

<article>Giving some Height to it.<br/>
Giving some Height to it.<br/>
Giving some Height to it.<br/>
Giving some Height to it.<br/>
</article>
&#13;
中
lirf
px追加儿童
查找答案关闭够了
当我使用绝对位置时,我的div重叠,如下面的代码所示:
<div id='get' style=''>kad</div>
<article >Giving some Height to it.<br/>
Giving some Height to it.<br/>
Giving some Height to it.<br/>
Giving some Height to it.<br/>
</article>
<script>
var ts = document.querySelectorAll('article')[0];
var ks = ts.offsetHeight;
var lirf = ks/2;
var kk = document.getElementById('get');
kk.style.position = "absolute";
kk.style.top=lirf+'px';
</script>
&#13;
答案 0 :(得分:0)
var ts = document.querySelectorAll('article')[0];
var ks = ts.offsetHeight;
var lirf = ks/2;
var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.position = 'absolute';
div.style.top= lirf+'px';
document.querySelectorAll('article')[0].appendChild(div);
希望这有帮助!
如果您的目标是垂直居中,请完成此https://vanseodesign.com/css/vertical-centering/