我想在“ addrow”按钮上方而不是下方添加具有bakkant的ID名称的div。同样由于某种原因,尽管它在Jsfiddle中运行得很好,但实际上在显示行之后立即消失了)。最好的方法是什么? https://jsfiddle.net/j3xazjak/3/
<div class="shape" id="plan4_shape">
<span class="bakkant">
<input class="measures" id="plan4_width" placeholder="mm"/><span class="times"> ×</span> <input class="measures" id="plan4_width" placeholder="mm"/>
</span>
<span class="bakkant" id="bakkant">
<input class="measures" id="plan4_width" placeholder="mm"/><span class="times"> ×</span> <input class="measures" id="plan4_width" placeholder="mm"/><button class="close" value="close1">×</button>
</span>
<button name="add_row" class="addrow" id="addrow4" onClick="addrow()">Add row</button>
</div>
jQuery的
$( document ).ready(function() {
document.getElementById('addrow4').onclick = duplicate;
var i = 0;
var original = document.getElementById('bakkant');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "bakkant" + ++i;
original.parentNode.appendChild(clone);
}
/* var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplicetor" + ++i; // there can only be one element with an ID
original.parentNode.appendChild(clone);
*/
/* jQuery('.four').before('<div class="addme">Add Me!</div>'); */
});
最佳答案
使用insertBefore
original.parentNode.insertBefore(clone, original);
关于jquery - 如何在按钮上方而不是下方添加重复的div,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34881838/