使用PHP动态生成具有不同ID的div [英] Dynamically Generate divs with different ids with PHP
问题描述
我想使用具有不同ID的PHP生成一定数量的div,我知道如何为一组数字生成它们,但是如何在点击时生成具有不同ID的div?另外,如果
我想删除div(及其对应的ID),该怎么做?
I want to generate a certain number of divs using PHP with different ids, I know how to generate them for a set number, but how do I generate on click, with different ids? Also, if I wanted to delete a div (and its corresponding id) how would I do that?
这是我要生成的代码(6 )div
This is the code I have for generating (6) divs
$element = "<div></div>";
$count = 6;
foreach( range(1,$count) as $item){
echo $element;
}
我在jquery / javscript中需要click()之类的东西(但在PHP中)来触发div的创建,而我什至都不知道从哪里开始。
I need something like the click() in jquery/javscript (but in PHP) to trigger div creation instead and I don't even know where to start.
推荐答案
在JavaScript中,您可以
In JavaScript you can do
function createDiv(id, parent)
{
var elem = document.createElement('div');
elem.id = id;
document.getElementById(parent).appendChild(elem);
}
createDiv(10, id-of-parent-elem-to-append-to);
其中10是新元素的ID,您必须提供该元素的ID作为第二个参数,应在其后附加新DIV
where 10 will be the ID of the new element and you will have to supply the ID of the element to which the new DIV should be appended, as the 2nd argument
这篇关于使用PHP动态生成具有不同ID的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!