我想要将一个DIV元素移动到另一个DIV元素内部。例如,我想要移动下面这个元素(包括所有子元素):
<div id="source">
...
</div>
变成这样:
<div id="destination">
...
</div>
以便我拥有这个:
<div id="destination">
<div id="source">
...
</div>
</div>
我想要将一个DIV元素移动到另一个DIV元素内部。例如,我想要移动下面这个元素(包括所有子元素):
<div id="source">
...
</div>
变成这样:
<div id="destination">
...
</div>
以便我拥有这个:
<div id="destination">
<div id="source">
...
</div>
</div>
appendTo
函数(该函数会将元素添加到末尾)。$("#source").appendTo("#destination");
或者您可以使用prependTo
函数(它会将内容添加到元素的开头):
$("#source").prependTo("#destination");
例子:
$("#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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">main</div>
<div id="moveMeIntoMain" class="moveMeIntoMain">move me to main</div>
<button id="appendTo">appendTo main</button>
<button id="prependTo">prependTo main</button>
它将被移动到目标位置(而不是克隆),并返回一个由插入元素组成的新集合
- http://api.jquery.com/appendto/ - John K我的解决方案:
移动:
jQuery("#NodesToMove").detach().appendTo('#DestinationContainerNode')
复制:
jQuery("#NodesToMove").appendTo('#DestinationContainerNode')
请注意 .detach() 的用法。在复制时,请注意不要复制 ID,以免重复。
使用原生JavaScript解决方案:
// Declare a fragment:
var fragment = document.createDocumentFragment();
// Append desired element to the fragment:
fragment.appendChild(document.getElementById('source'));
// Append fragment to desired element:
document.getElementById('destination').appendChild(fragment);
尝试使用纯JavaScript?destination.appendChild(source);
。
addEventListener("click", () => document.getElementById("destination").appendChild(document.getElementById("source")));
div {
margin: .1em;
}
#destination {
border: solid 1px red;
}
#source {
border: solid 1px gray;
}
<div id="destination">
###
</div>
<div id="source">
***
</div>
$("#destination").append($("#source"));
div
中,并且希望在主要内容之前展示该元素:$("#destination").prepend($("#source"));
div
是空的,或者您想要完全替换它:$("#element").html('<div id="source">...</div>');
$("#destination").append($("#source").clone());
// etc.
您可以使用:
在后面插入,
jQuery("#source").insertAfter("#destination");
插入到另一个元素内部:
jQuery("#source").appendTo("#destination");
jQuery("#source")
.detach()
.appendTo('#destination');
试用工作中的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>
如果您想快速了解如何移动元素并获取更多详细信息,请尝试访问以下链接:
http://html-tuts.com/move-div-in-another-div-with-jquery
这里是一个简短的示例:
要在元素上方移动:
$('.whatToMove').insertBefore('.whereToMove');
移动一个元素之后:
$('.whatToMove').insertAfter('.whereToMove');
为了在元素内部移动,首先要移动到容器内所有元素的上方:
$('.whatToMove').prependTo('.whereToMove');
在一个容器内,移动到该容器中的所有元素之后的位置:
$('.whatToMove').appendTo('.whereToMove');
我需要将内容从一个容器移动到另一个容器,包括所有的事件监听器。jQuery 没有这样的方法,但标准的 DOM 函数 appendChild 有。
// Assuming only one .source and one .target
$('.source').on('click',function(){console.log('I am clicked');});
$('.target')[0].appendChild($('.source')[0]);
使用 appendChild 可以移动 .source* 并且包括它的事件监听到目标中: Node.appendChild() (MDN)