将 .slideToggle 与动态生成的 div 一起使用
我在显示 MySQL 查询的 while 循环中创建了 div。我想用 .slideToggle 隐藏和显示它们。我不能使用一个类,因为这会触发页面上的每个特定 div 向下滑动。
我认为这会在 while 循环中重复并找到最接近的toggleSectionDyn div id。显然不是。
<div class="actions"><a href="#" id="toggleButtonDyn">Add</a></div>
<div id="toggleSectionDyn">Some content</div>
<script>
$("#toggleButtonDyn").click(function(){
$(this).closest("#toggleSectionDyn").slideToggle("slow");
return false;
});
</script>
I have divs created within a while loop displaying a MySQL query. I'd like to hide and show them with .slideToggle. I can't use one class as that would trigger every specific div on the page to slide down.
I thought this would repeat within the while loop and find the closest toggleSectionDyn div id. Obviously not.
<div class="actions"><a href="#" id="toggleButtonDyn">Add</a></div>
<div id="toggleSectionDyn">Some content</div>
<script>
$("#toggleButtonDyn").click(function(){
$(this).closest("#toggleSectionDyn").slideToggle("slow");
return false;
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑: live 已被弃用。考虑使用 .on()
你想使用
.live( ) API
现在,在重新阅读时,我想知道您到底在问什么。您还必须确保每个
必须具有唯一的 ID。您可能会考虑
.next('div')
而不是.closest()
根据评论中的讨论,我会使用 .next('div') 看看修改了昨天问题中的jsfiddle http://jsfiddle.net/ycpgD/
另外,我强烈建议使用唯一ID 。如果它们不能是唯一的,则将它们分类。
EDIT: live is deprecated. consider using .on()
you want to use
.live()
from the .live() api
Now, upon re-reading, I wonder exactly what your asking. You must also make sure each
<div>
must have a unique ID. You may consider you.next('div')
instead of.closest()
Per the discussion in comments, I would use .next('div') look at the modified jsfiddle from yesterdays question http://jsfiddle.net/ycpgD/
also, I would strongly recommend using unique ID's. if they cannot be unique make them classes.
如果你稍微重构一下,你就可以定位你想要的 div。 (也许我在这里错过了这个问题,但我的解决方案非常简单)。
http://jsfiddle.net/8nXvX/
我想这取决于你在哪里生成
< ;a>
。是否要隐藏整个 MySQL 结果集,或者一次显示/隐藏每一行?You can target the div you're after, if you refactor a little. (Maybe I'm missing the issue here, but my solution is pretty straight forward).
http://jsfiddle.net/8nXvX/
I suppose it depends where you're generating that
<a>
. If it's a going to hide the entire MySQL result set, or is it going to show/hide each row one at a time?