如何在布局的其余部分不受 div 元素影响的情况下将 div 放置在另一个元素下?
<div style="margin:50px">
<div style="margin:20px; padding:10px; width:100px">
here is a little <span id="test" style="font-weight:bold">test</span>.. some text some text some text some text
</div>
</div>
<script>
var elm = document.getElementById('test');
var div = elm.appendChild(document.createElement('div'));
with(div){
style.position = 'relative';
style.left = elm.offsetLeft+'px';
style.background = '#ffffff';
style.width = '100px';
style.height = '50px';
innerHTML = 'wooop';
}
</script>
最佳答案
使用 css 属性 z-index 或 javascript 样式属性 zIndex。
z-index 定义层的高度。 Canvas (通常是 body 标签)使用 z-index 0。更高的在 body 之上,更低的在 body 下面。
CSS 示例:
<style type="text/css">
.my_layer {
display: block;
position: absolute;
z-index: 10;
top: 10px;
left: 10px;
width: 100px;
height: 100px;
}
</style>
javascript 示例:
<script type="text/javascript">
el = document.getElementById("my_div")
with(el) {
style.zIndex = 10;
style.display = "block";
style.position = "absolute";
style.top = "10px";
style.left = "10px";
style.width = "100px";
style.height = "100px";
}
</script>
关于html - 将一个 div 放在另一个元素下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4857217/