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
3 Answers
Reset to default 4In 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
版权声明:本文标题:javascript - Dynamically Generate divs with different ids with PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034622a2417105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论