javascript 将 div 元素从一个父 div 移动到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10905525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move div element from one parent div to another
提问by HunderingThooves
So let's assume that I have a set of nested divs:
所以让我们假设我有一组嵌套的 div:
<div id="likelyToBeCalled">
<div id="likelyOddHeader" class="row">
<div id="likelyOddA" class="left" name="test1">Test1</div>
<div id="LikelyOddB" class="middle"><img src="image002.png"/></div>
<div id="timeZone" class="right">West</div>
</div>
and further down the page:
并在页面下方:
<div id="unlikelyToBeCalled">
<div id="likelyOddHeader" class="row">
<div id="likelyOddA" class="left">Test2</div>
<div id="LikelyOddB" class="middle"><img src="image002.png"/></div>
<div id="timeZone" class="right">West</div>
</div>
How would I move Test1 to "unlikelyToBeCalled". I've been trying this with a form / submit button just for kicks, here's the code for that:
我将如何将 Test1 移动到“unlikelyToBeCalled”。我一直在用一个表单/提交按钮来尝试这个,只是为了踢,这是代码:
<script type="text/javascript">
function doSubmit() {
document.getElementById('unlikelyToBeCalled').appendChild(
document.getElementsByTagName('Test1')
);
}
</script>
<br /><br /><br />
<form method="POST" action="file:///C:/wamp/www/index.html" id="submitform" name="submitform">
<input type="submit" name="Submit" value="Move divs" onClick="doSubmit()" />
</form>
Or something to that effect. Any help would rock
或者类似的东西。任何帮助都会摇滚
回答by Musa
Use .appendTo()
利用 .appendTo()
$('#likelyOddA').appendTo('#unlikelyToBeCalled')
回答by Wirone
If I understand what you want:
如果我明白你想要什么:
$('div[name=test1]').appendTo('#unlikelyToBeCalled');
getElementsByTagName('Test1')
will not get element with name="Test1"
, it is supposed to, for example, get all div
s (with code getElementsByTagName('div')
of course). Next, you have used #likelyOddHeader
and other id
s twice, but id
must be unique.
getElementsByTagName('Test1')
不会用 获取元素name="Test1"
,例如,它应该获取所有div
s(getElementsByTagName('div')
当然还有代码)。接下来,您使用了#likelyOddHeader
and otherid
两次,但id
必须是唯一的。