我有多个 div,它们是绝对定位的,并通过单击按钮动态创建。单击后,它们将放置在相对定位的容器中,我在每个 div 上都有一个按钮,单击它时将删除 div,问题是删除 div 时,它会影响其他位置。
function create() {
var $home = $('<div class="cabine"></div>');
$("#container").append($home);
}
.cabine { /*class that all div's share*/
position: absolute;
top:5%;
left:10%;
width:135px;
height:135px;
float:left;
background: red;
}
#container { /* Where the div's are placed*/
position: relative;
background-image: url(wall.jpg);
background-color: #FFFFFF;
border: 1px solid #000;
width: 100%;
height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="create()">Create Cabine</button>
<div id="container"></div>
最佳答案
在 html 中你有 #container
而不是 .container
。
关于javascript - 绝对定位的元素在删除时影响另一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999536/