简体   繁体   English

如何将div克隆放置在原始div下?

[英]How to place div clone right under original div?

I currently have an "Add more" button that when clicked, duplicates a div I have. 我目前有一个“添加更多”按钮,单击该按钮可复制我的div。 The problem currently is when the button is clicked and the div duplicates, it appears below the "Add more" button. 当前的问题是单击该按钮并且div复制时,它出现在“添加更多”按钮的下方。 I want the div to duplicate right under the original div when clicked, this also means the duplicate will be above the "Add more" button. 我希望div在单击时在原始div的正下方重复,这也意味着重复将在“添加更多”按钮上方。 How do I go about doing this? 我该怎么做呢?

HTML HTML

                    <!-- Instrument Exp. -->
                    <div>
                        <label>What instruments and level do you want to teach?</label>
                        <div class="form-inline justify-content-center" id="ins_1">
                            <!-- Instrument 1 -->
                            <div>
                                <select class="form-control ins">
                                    <option>Instrument</option>
                                    <option>Cello</option>
                                    <option>Clarinet</option>
                                    <option>Drums</option>
                                    <option>Flute</option>
                                    <option>Guitar</option>
                                    <option>Piano</option>
                                    <option>Saxophone</option>
                                    <option>Violin</option>
                                    <option>Vocal</option>
                                    <option>Trumpet</option>
                                </select>
                            </div>

                            <!-- Level 1 -->
                            <div>
                                <select class="form-control lvl">
                                    <option>Level</option>
                                    <option>Beginner</option>
                                    <option>Advanced</option>
                                    <option>Intermediate</option>
                                </select>
                            </div>
                        </div>


                    <!-- Add More -->
                    <div class="text-center" id="add_more">
                        <button type="button" class="no_link" onclick="add_instrument()">Add more +</button>
                    </div>
            </div>

JavaScript JavaScript的

var i = 0;
var original = document.getElementById('ins_1');

function add_instrument() {
    var clone = original.cloneNode(true);
    clone.id = "ins_1" + ++i;
    original.parentNode.appendChild(clone);
}

See insertBefore, https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore . 请参见insertBefore, https://developer.mozilla.org/zh-CN/docs/Web/API/Node/insertBefore If you combine it with nextSibling, you can have it add right after it: 如果将其与nextSibling结合使用,则可以在其后添加:

var i = 0;
var original = document.getElementById('ins_1');
function add_instrument() {
    var clone = original.cloneNode(true);
    clone.id = "ins_1" + ++i;
    original.parentNode.insertBefore(clone, original.nextSibling);
}

Also see: How to insert an element after another element in JavaScript without using a library? 另请参见: 如何在不使用库的情况下在JavaScript中的另一个元素之后插入一个元素?

Edit : As @Tej pointed out in the comments, there are nice functions to insert nodes adjacently: insertAdjacentHTML , and insertAdjacentElement . 编辑 :正如@Tej在评论中指出的那样,有一些不错的函数可用于相邻插入节点: insertAdjacentHTMLinsertAdjacentElement insertAdjacentElement requires IE8+ or a recent browser. insertAdjacentElement需要IE8 +或最新的浏览器。

The code becomes more readable with those functions, in my opinion: 我认为这些功能使代码更具可读性:

var i = 0;
var original = document.getElementById('ins_1');
function add_instrument() {
    var clone = original.cloneNode(true);
    clone.id = "ins_1" + ++i;
    original.insertAdjacentElement('afterend', clone);
}

appendChild inserts the node as the last child of the node. appendChild将节点插入为节点的最后一个子节点。 In this case, that's after the 'add more' button because your list and button are children within the same element. 在这种情况下,它在“添加更多”按钮之后,因为您的列表和按钮是同一元素中的子元素。

Solution 1 解决方案1

Put your list of new instruments in a different element so appending to the end of the parent has the desired effect. 将您的新乐器列表放在不同的元素中,以便在父乐器的末尾附加效果。

<div>
    <label>What instruments and level do you want to teach?</label>
    <div class="items">
        <div class="form-inline justify-content-center" id="ins_1">
            ...
        </div>
    </div>

    <!-- Add More -->
    <div class="text-center" id="add_more">
        <button type="button" class="no_link" onclick="add_instrument()">Add more +</button>
    </div>
</div>

Solution 2 解决方案2

Use insert before and insert before the "add more button". 在“添加更多按钮”之前和之前插入。

original.parentNode.insertBefore(clone, addMoreButton);

Solution 3 (not recommended) 解决方案3(不建议)

Append the "add more" button appending the new instrument item to place it back at the end of the list. 追加新仪器项后的“添加更多”按钮,将其放回列表的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM