How to move an element into another element?

前端 未结 15 2240
無奈伤痛
無奈伤痛 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:35

    You may want to use the appendTo function (which adds to the end of the element):

    $("#source").appendTo("#destination");
    

    Alternatively you could use the prependTo function (which adds to the beginning of the element):

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

    Example:

    $("#appendTo").click(function() {
      $("#moveMeIntoMain").appendTo($("#main"));
    });
    $("#prependTo").click(function() {
      $("#moveMeIntoMain").prependTo($("#main"));
    });
    #main {
      border: 2px solid blue;
      min-height: 100px;
    }
    
    .moveMeIntoMain {
      border: 1px solid red;
    }
    
    
    main
    move me to main

提交回复
热议问题