Dynamically Generate divs with different ids with PHP

删除回忆录丶 提交于 2019-12-11 18:19:31

问题


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?

This is the code I have for generating (6) divs

 $element = "<div></div>";
    $count = 6;
    foreach( range(1,$count) as $item){
        echo $element;
    }

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.


回答1:


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);

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




回答2:


echo "<div id='$item'></div>";

instead?




回答3:


Ok, to create them with different ids you can do something like this:

$element = "<div id=";
$count = 6;
foreach($id=0;$id<$count;$id++) {
    echo $element."div".$id."></div>";
}

In the same way as you appended the id you can append an onClick event that says something like this:

onclick="this.style='visibility:hidden;'";

or something along those lines. Hope this helps.



来源:https://stackoverflow.com/questions/12084994/dynamically-generate-divs-with-different-ids-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!