【发布时间】:2015-08-28 11:57:41
【问题描述】:
我有一个问题,当我通过按下输入按钮添加 div 时,它(按钮)也会随着我第一次添加的 div 而下降。
代码如下:
$(document).ready(function() {
var count = 0;
$("#addBut").click(function() {
$('body').prepend('<div class="content" id="first' + count + '"><h3>Heading tag</h3><p>Paragraph tag</p></div>');
//It is the jquery code div which I prepends
count++;
});
$("#TextBut")
.click(function() {
var value = $(inpText).val();
$("#para").text(value);
})
});
#addBut {
//style of button which goes down
display: block;
padding: 3px 10px;
cursor: pointer;
margin: auto;
}
.content {
//style of div which is to be prepend
position: relative;
background-color: white;
width: 500px;
height: 350px;
padding: 25px;
border: 5px solid black;
display: block;
margin-left: auto;
margin-right: auto;
top: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="text" id="inpText">
<textarea rows="12" cols="50">//it is text area which also goes down TextArea Location
</textarea>
<input type="button" id="addBut" value="Add">// HTML tag of Button which goes down
<input type="button" id="TextBut">
</div>
谁能帮助我,当某些特定元素被添加时,如何避免其他内容不改变它们的位置?
【问题讨论】:
-
你试过追加吗?
-
抱歉,我使用了 prepend。
-
追加可能不会发生这种情况。
-
'); count++; }); $("#delBut").click(function(){ $("#first5").remove(); }); });
使用上面的 JQUERY 代码加上 prepend 来检查 div 的确切位置。我希望在正文顶部添加新的 div