2010-09-16 80 views
0

我现在有这个权利:jQuery:“显示所有评论”点击并隐藏成功?

<a href='javascript:showComments($displayWall[id]);' style='cursor: pointer;'>Show all $isThereAnyComments comments</a> 

论showComments成功,我想让它显示这个div:

#showWallCommentsFor + wallID 

然后当它显示的话,就应该改变“全部显示”,以“关闭”,然后它应该隐藏div。我怎样才能做到这一点?

+2

一个建议(不会直接解决您的问题) - 不要你的HTML中包含Javascript。 – JasCav 2010-09-16 14:27:36

+0

为什么不呢?什么是激活showComments()的另一种方式? – Karem 2010-09-16 14:29:41

+0

onclick()事件... – Stefanvds 2010-09-16 14:31:00

回答

1
var toggle = false; 
$(function() { 
    $('a.comments').click(function(e) { 
     var $this = $(this); 
     $('.toggleComments').toggle(1000,function() { 
      if(!toggle) { 
       $this.text('Hide Comments'); 
       toggle = !toggle; 
      }else { 
       $this.text('Show Comments'); 
       toggle = !toggle; 
      } 
     }); 
     e.preventDefault(); 
    }); 
}); 

测试上面的代码@http://jsbin.com/azuro3

0

HTML:

<div id="comments"> 
    <!-- comments here --> 
</div> 
<a href="#" class="commentsBtn">Show Comments</a> 

的Jquery:

//Start with comments hidden 
$('#comments').hide(); 
//Attach click event to button 
$('.commentsBtn').click(function() { 
    $('#comments').slideToggle('slow'); 
    $(this).text($(this).text() == 'Hide Comments' ? 'Show Comments' : 'Hide Comments'); 

    //Kill the default function of the anchor tag 
    return false; 
}); 

CSS:

.commentsBtn { cursor: pointer; } 
  • 内联应该尽可能避免,在这种情况下,JavaScript和CSS。
  • 请确保您的评论有一个容器div,并指定id或class,也许id =“comments”。
  • 添加一个类你的节目评论的链接,你可以用它来申请你的CSS样式,并在你的jQuery选择。

如果让你用它打通我知道!所有最优秀的