我在DIV中添加了几个按钮。
var newVr = "";
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
document.getElementById('extraDIV')innerHTML += newVr;不是将按钮添加到按钮的末尾,而是如何在开始时添加新按钮?
添加新按钮后的预期输出。
新老旧
发布于 2015-07-07 11:48:44
您可以使用前额()
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>
发布于 2015-07-07 11:49:02
因为您已经用jquery标记了这个问题。
for (i = 0; i < 5; i++) {
$("#extraDIV").prepend('<button type="button" class="abc">New</button>');
}发布于 2015-07-07 11:50:05
试着使用前额(),
var newVr = "";
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
$('#extreDIV').prepend(newVr);
var newVr = "";
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New '+(i+1)+'</button>';
}
$('#extreDIV').prepend(newVr);<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="extreDIV"></div>
https://stackoverflow.com/questions/31267706
复制相似问题