How to move an element into another element?

前端 未结 15 2264
無奈伤痛
無奈伤痛 2020-11-22 03:35

I would like to move one DIV element inside another. For example, I want to move this (including all children):

...
相关标签:
15条回答
  • 2020-11-22 04:10

    For the sake of completeness, there is another approach wrap() or wrapAll() mentioned in this article. So the OP's question could possibly be solved by this (that is, assuming the <div id="destination" /> does not yet exist, the following approach will create such a wrapper from scratch - the OP was not clear about whether the wrapper already exists or not):

    $("#source").wrap('<div id="destination" />')
    // or
    $(".source").wrapAll('<div id="destination" />')
    

    It sounds promising. However, when I was trying to do $("[id^=row]").wrapAll("<fieldset></fieldset>") on multiple nested structure like this:

    <div id="row1">
        <label>Name</label>
        <input ...>
    </div>
    

    It correctly wraps those <div>...</div> and <input>...</input> BUT SOMEHOW LEAVES OUT the <label>...</label>. So I ended up use the explicit $("row1").append("#a_predefined_fieldset") instead. So, YMMV.

    0 讨论(0)
  • 2020-11-22 04:11

    I just used:

    $('#source').prependTo('#destination');
    

    Which I grabbed from here.

    0 讨论(0)
  • 2020-11-22 04:11

    You can use pure JavaScript, using appendChild() method...

    The appendChild() method appends a node as the last child of a node.

    Tip: If you want to create a new paragraph, with text, remember to create the text as a Text node which you append to the paragraph, then append the paragraph to the document.

    You can also use this method to move an element from one element to another.

    Tip: Use the insertBefore() method to insert a new child node before a specified, existing, child node.

    So you can do that to do the job, this is what I created for you, using appendChild(), run and see how it works for your case:

    function appendIt() {
      var source = document.getElementById("source");
      document.getElementById("destination").appendChild(source);
    }
    #source {
      color: white;
      background: green;
      padding: 4px 8px;
    }
    
    #destination {
      color: white;
      background: red;
      padding: 4px 8px;
    }
    
    button {
      margin-top: 20px;
    }
    <div id="source">
      <p>Source</p>
    </div>
    
    <div id="destination">
      <p>Destination</p>
    </div>
    
    <button onclick="appendIt()">Move Element</button>

    0 讨论(0)
  • 2020-11-22 04:19

    If you want a quick demo and more details about how you move elements, try this link:

    http://html-tuts.com/move-div-in-another-div-with-jquery


    Here is a short example:

    To move ABOVE an element:

    $('.whatToMove').insertBefore('.whereToMove');
    

    To move AFTER an element:

    $('.whatToMove').insertAfter('.whereToMove');
    

    To move inside an element, ABOVE ALL elements inside that container:

    $('.whatToMove').prependTo('.whereToMove');
    

    To move inside an element, AFTER ALL elements inside that container:

    $('.whatToMove').appendTo('.whereToMove');
    
    0 讨论(0)
  • 2020-11-22 04:19

    You can use following code to move source to destination

     jQuery("#source")
           .detach()
           .appendTo('#destination');
    

    try working codepen

    function move() {
     jQuery("#source")
       .detach()
       .appendTo('#destination');
    }
    #source{
      background-color:red;
      color: #ffffff;
      display:inline-block;
      padding:35px;
    }
    #destination{
      background-color:blue;
      color: #ffffff;
      display:inline-block;
      padding:50px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="source">
    I am source
    </div>
    
    <div id="destination">
    I am destination
    </div>
    
    <button onclick="move();">Move</button>

    0 讨论(0)
  • 2020-11-22 04:19

    Old question but got here because I need to move content from one container to another including all the event listeners.

    jQuery doesn't have a way to do it but standard DOM function appendChild does.

    //assuming only one .source and one .target
    $('.source').on('click',function(){console.log('I am clicked');});
    $('.target')[0].appendChild($('.source')[0]);
    

    Using appendChild removes the .source and places it into target including it's event listeners: https://developer.mozilla.org/en-US/docs/Web/API/Node.appendChild

    0 讨论(0)
提交回复
热议问题