jQuery 我可以将一个已经存在的 div 附加到另一个已经存在的 div 上吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11152315/
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
Can i append an already existing div to another already existing div?
提问by Patsy Issa
I have a contact form in a div on it's own with opacity 0, and a div where content is dynamically manipulated depending on what the user click on the menu. After the user gets to the last stage of the menu i need to clear the content of the div that displays everything and then "move" the form div into it, would something like this work?
我在自己的 div 中有一个联系表单,不透明度为 0,还有一个 div,其中的内容根据用户单击菜单的内容进行动态操作。在用户进入菜单的最后阶段后,我需要清除显示所有内容的 div 内容,然后将表单 div“移动”到其中,这样的事情会起作用吗?
$('#menu_form').on('click', function() {
$('#form_div').append('#display_div');
});
So to recap 2 already existing divs, need to place one of them into the other on click.
因此,要回顾 2 个已经存在的 div,需要在单击时将其中一个放入另一个中。
回答by Roko C. Buljan
.appendTo()
.appendTo()
$('#menu_form').on('click', function(){
$('#form_div').appendTo('#display_div'); // appendTo -> selector
});
.append()
.append()
$('#menu_form').on('click', function(){
$('#display_div').append( $('#form_div') ); // append -> object
});
回答by Richard Neil Ilagan
Check this jsFiddlefor a quick POC. Apparently it does.
检查此 jsFiddle以获取快速 POC。显然确实如此。
The trick is to pass the object reference, not just the object id, like so:
诀窍是传递对象引用,而不仅仅是对象 id,如下所示:
$('#menu_form').on('click', function(){
$('#form_div').append($('#display_div'));
});
You could also pass the current object, using this
:
您还可以传递当前对象,使用this
:
$('#menu_form').on('click', function(){
$('#form_div').append(this);
});