admin管理员组

文章数量:1317898

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.

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.

Share Improve this question edited Aug 23, 2012 at 5:18 Toon Krijthe 53.5k38 gold badges149 silver badges203 bronze badges asked Aug 23, 2012 at 4:59 Atom VayalinkalAtom Vayalinkal 2,7028 gold badges30 silver badges37 bronze badges 2
  • 2 PHP can't handle clicking. You probably want Javascript. – Waleed Khan Commented Aug 23, 2012 at 5:00
  • because it's server side, right? how would i do this in javascript then – Atom Vayalinkal Commented Aug 23, 2012 at 5:05
Add a ment  | 

3 Answers 3

Reset to default 4

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

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

instead?

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.

本文标签: javascriptDynamically Generate divs with different ids with PHPStack Overflow