该页面的代码如下
<?php foreach ($organization->index() as $id=>$content) { ?>
<a href="#" id="obtain_branch"><?= $content->name?></a>
<div id="directory_brnach"><?= $content->about?></div>
<?php } ?>
使用jQuery的JavaScript代码是
// Directory inner branches
$('#obtain_branch').click(function(){
$('#directory_brnach').toggle('fast');
});
问题只会在第一个问题上有效,因为ID不会改变,其余的都不会改变。
如何将数组发送到jQuery函数以在每次迭代时提供单独的ID?
提前致谢。
答案 0 :(得分:2)
#obtain_branch
选择具有该ID的第一个元素。使用类将有助于链接,但不会用于div
- 这将立即切换它们。
试试:
<?php foreach ($organization->index() as $id=>$content) { ?>
<a href="#" class="obtain_branch"><?= $content->name?></a>
<div class="directory_brnach"><?= $content->about?></div>
<?php } ?>
JavaScript的:
$('.obtain_branch').click(function(){
$(this).next('.directory_brnach').toggle('fast');
return false; //to avoid the link from working
});
答案 1 :(得分:2)
ID必须是唯一的。尝试:
<?php foreach ($organization->index() as $id=>$content) { ?>
<a href="#" class="obtain_branch"><?= $content->name?></a>
<div><?= $content->about?></div>
<?php } ?>
使用:
$('a.obtain_branch').click(function(){
$(this).next().toggle('fast');
return false;
});