我在 DIV 中添加了几个按钮。
var newVr = "";
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
document.getElementById('extraDIV')innerHTML += newVr;
如何在开头添加新按钮,而不是将按钮添加到按钮的末尾?
添加新按钮后的预期输出,例如
[新][新][旧][旧][旧][旧][旧][旧]
最佳答案
您可以使用 prepend()
for(i=0; i<5; i++){
$('#extreDIV').prepend('<button type="button" class="abc">New</button>');
}
for (i = 0; i < 5; i++) {
$('#extreDIV').prepend('<button type="button" class="abc">New</button>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="extreDIV">
<button type="button" class="abc">Old</button>
<button type="button" class="abc">Old</button>
<button type="button" class="abc">Old</button>
</div>
关于javascript - 在开头添加动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31267706/