5

我正在编写自己的轻量级博客平台(我正在尝试学习 PHP 和 jQuery,所以我不只是想使用 Wordpress)。使用 PHP,我有一个分页系统,每页显示 5 篇博客文章。在我的 while 循环中,我想要一个链接,上面写着“发表评论”,点击它时,将打开一个 DIV,其中有一个文本框来输入评论。我正在使用的当前代码在页面上打开了所有 5 个评论 DIV。我需要能够为每个评论 DIV 提供一个唯一 ID(基于我假设的博客文章的 ID)并将其放在我的 jquery 切换功能中,以便在单击链接时只打开一个评论 DIV,而不是所有他们。谁能帮帮我吗?

这是我的 jQuery,它在页面上打开了所有切换的 div:

<script type="text/javascript"> 
   $(document).ready(function() {  
     $(".toggleCommentBox").click(function() {
       $(".commentBox").toggle()
     });  
   });      
</script>

这是我的 while 循环中的代码,它显示了博客文章和链接:

<a href = "#" class = "toggleCommentBox">Leave a Comment</a>

<div class = "commentBox" style = "display:none;">
    ...Form stuff in here
</div>

我不需要评论框 div 中的表单内容的帮助,我只需要 jQuery 的帮助以使页面上的 5 个评论框中的每一个都独一无二,并且所有这些框都可以单独切换,而不是一个链接打开所有5 在页面上切换 DIV,就像现在发生的那样。任何人都可以给我的任何帮助将不胜感激。

4

2 回答 2

6

尝试这样的事情

<script type="text/javascript"> 
 $(document).ready(function() {  
 $(".toggleCommentBox").each(function{

   var getId = $(this).attr('getId');
   $(this).click(function(){

        $("#commentBox"+getId).toggle();
     })
  })
});      

<a href = "#" class = "toggleCommentBox" getId='1' >Leave a Comment</a>

<div class = "commentBox" style = "display:none;" id="commentBox1">
...Form stuff in here
</div>

希望你明白我想说什么

于 2013-03-22T05:53:42.573 回答
4

使用 jquery 下一个函数:

<script type="text/javascript"> 
   $(document).ready(function() {  
     $(".toggleCommentBox").click(function() {
       $(this).next(".commentBox").toggle()
     });  
   });      
</script>

http://api.jquery.com/next/

于 2013-03-22T05:45:45.483 回答