[英]How do you use Javascript to add or remove HTML/CSS code?
If I have a bunch of HTML code, similar to the following:如果我有一堆 HTML 代码,类似于以下内容:
<div id='test0div'>
<p id='test0'></p>
</div>
How do I use JavaScript to add or remove more of those - ie我如何使用 JavaScript 添加或删除更多的那些 - 即
<div id='test1div'>
<p id='test1'></p>
</div>
<div id='test2div'>
<p id='test2'></p>
</div>
...etc.? ...等等。?
To remove you could use要删除您可以使用
$('#id').remove();
To add you could use添加你可以使用
$("<div id='new'></div>").appendTo('#id');
var container = document.createElement("div");
for(var i=0; i<5; i++) { // change i <5 as per your data source
var div = document.createElement("div");
var p = document.createElement("p");
p.id = "test"+i;
div.id = "test"+i+"div";
div.appendChild(p);
container.appendChild(div); // you can event append to particular id or body
}
// document.getElementById("divId").appendChild(container);
container
, will have all the divs & p as you wish This will give you the output you want. container
,将拥有您希望的所有 divs & p 这将为您提供所需的输出。 Just change the number of times the loop will execute based on your wish.只需根据您的意愿更改循环执行的次数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.