使用 javascript 或 jquery 将 Div 动态添加到 html 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10954911/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:36:49 来源:igfitidea点击:
Dynamically add Div to html page using javascript or jquery
提问by jardane
I want to have a main div and have the ability to dynamically add new divs at the same level as the main div. Something like this:
我想要一个主 div 并且能够动态添加与主 div 相同级别的新 div。像这样的东西:
<div id="main"></div>
<div id="created_div"></div>
Any help would be wonderful
任何帮助都会很棒
回答by Zoltan Toth
$("#parent_div").append('<div id="created_div"></div>');
or if you want the newly created <div>
-s to appear before the others
或者如果您希望新创建的<div>
-s 出现在其他人之前
$("#parent_div").prepend('<div id="created_div"></div>');
回答by emphaticsunshine
$('#main').after('<div id="created_div"></div>');
回答by Akhi
$('<div id="created_div"></div>').insertAfter('#main');