以下代码在单击链接时显示一个 div,但是它会为我的新闻提要页面上的每个帖子打开每个评论 div。有没有办法只打开与其新闻帖子特别相关的评论 div?
HTML
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Avinash</title>
<style>
</style>
</head>
<body>
<a href="#" class="comments">Show Comments</a>
<br />
<div class="toggleComments" style="display:none;">
This is #comment1 <br />
This is #comment2 <br />
This is #comment3 <br />
This is #comment4 <br />
This is #comment5 <br />
This is #comment6
</div>
</body>
</html>
Javascript
var toggle = false;
$(function() {
$('a.comments').click(function(e) {
var $this = $(this);
$('.toggleComments').toggle(500,function() {
if(!toggle) {
$this.text('Hide Comments');
toggle = !toggle;
}else {
$this.text('Show Comments');
toggle = !toggle;
}
});
e.preventDefault();
});
});
任何帮助表示赞赏。