如何在多个显示的评论中显示/隐藏回复评论框

时间:2017-04-17 06:57:27

标签: javascript jquery html

所以这里是我使用这个代码来显示/隐藏它实际工作的评论框但是当我试图点击第一条评论时评论框显示/隐藏时发出多条评论时遇到的问题但当我试图点击其他评论它不再工作它只适用于第一个....有没有办法解决这个问题?...

HTML代码

<div id="replybutton" class="btn4 like" style="margin-top:-25px;margin-left:-10px;">
    <span class="btn reply" id="replyb">Reply</span> •&emsp;
</div>

<div class="col-lg-12" id="reply" style="background:#eff9c7;margin-left:50px;margin-top:-20px;display:none;" >
    <img  src="img/icons/no-icon-available.png" class="pull-left" style="   border-radius:50%;margin-bottom:3px;width:30px;height:30px;" />
    <p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;">Hajime Sasota </strong> Sample comment<br>

<div class="btn4 disabled" style="margin-top:-25px;margin-left:-10px;">
    <span style="margin-left:5px;font-size:12px;color:gray;"><abbr class="timeago" title="">time</abbr></span>
</div>
</p>
<input type="text" class="form-control pull-right" style="width:88%;height:25px;margin-top:-10px;" placeholder="Write a reply..." />
</div>

JAVASCRIPT

<script type="text/javascript">
$(document).ready(function(){
$('#replybutton').click(function(){
$('#reply').toggle();
});
});
</script> 

1 个答案:

答案 0 :(得分:1)

id在HTML页面中必须是唯一的,您应该将ID更改为类replybuttonreply,jquery代码将是,

$(document).ready(function(){
    $('.replybutton').click(function(){
       $(this).next('.reply').toggle();
    });
});

<强>代码段,

&#13;
&#13;
$(document).ready(function() {
  $('.replybutton').click(function() {
    $(this).next('.reply').toggle();
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="container">
  <div class="replybutton btn4 like" style="">
    <span class="btn reply" id="replyb">Reply</span> •&emsp;
  </div>

  <div class="col-lg-12 reply" style="display:none">
    <img src="img/icons/no-icon-available.png" class="pull-left" style="" />
    <p>
      <strong class="font-1" style="margin-left:10px;">Hajime Sasota </strong> Sample comment<br/>

      <div class="btn4" style="">
        <span style=""><abbr class="timeago" title="">time</abbr></span>
      </div>
    </p>
    <input type="text" class="form-control pull-right" placeholder="Write a reply..." />
  </div>
</div>
<div class="container">
  <div class="replybutton btn4 like" style="">
    <span class="btn reply" id="replyb">Reply</span> •&emsp;
  </div>

  <div class="col-lg-12 reply" style="display:none">
    <img src="img/icons/no-icon-available.png" class="pull-left" style="" />
    <p>
      <strong class="font-1" style="margin-left:10px;">Hajime Sasota </strong> Sample comment<br/>

      <div class="btn4" style="">
        <span style=""><abbr class="timeago" title="">time</abbr></span>
      </div>
    </p>
    <input type="text" class="form-control pull-right" placeholder="Write a reply..." />
  </div>
</div>
<div class="container">
  <div class="replybutton btn4 like" style="">
    <span class="btn reply" id="replyb">Reply</span> •&emsp;
  </div>

  <div class="col-lg-12 reply" style="display:none">
    <img src="img/icons/no-icon-available.png" class="pull-left" style="" />
    <p>
      <strong class="font-1" style="margin-left:10px;">Hajime Sasota </strong> Sample comment<br/>

      <div class="btn4" style="">
        <span style=""><abbr class="timeago" title="">time</abbr></span>
      </div>
    </p>
    <input type="text" class="form-control pull-right" placeholder="Write a reply..." />
  </div>
</div>
&#13;
&#13;
&#13;