jquery 实现列表点击折叠与展开的功能

需求是点击列表,展开折叠的内容,再次点击后折叠列表,还要给数据打上已查看的标记,折叠列表后页面刷新,有已查看标记的数据就不会显示.

jquery代码:

    $(function (){
    var aa = 0;
 $(".gradeX").click(function(){
 $(this).toggleClass("selected");
    var b = $(this).find('.hostid').html();
    var c = $(this).find('.catas').html();
    if (aa == 0 ) {
 $(this).siblings().not(".gradeX").not(":first-child").hide();
 $(this).next().show
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的示例代码: HTML: ```html <div class="comments"> <h3>评论</h3> <ul class="comment-list"> <li> <div class="comment-header"> <span class="comment-author">张三:</span> <span class="comment-time">2021-01-01 11:11:11</span> <a href="#" class="comment-reply">回复</a> </div> <div class="comment-content"> 这是一条评论。 </div> <ul class="children"> <li> <div class="comment-header"> <span class="comment-author">李四:</span> <span class="comment-time">2021-01-02 12:12:12</span> <a href="#" class="comment-reply">回复</a> </div> <div class="comment-content"> 这是一条回复。 </div> </li> </ul> </li> </ul> <div class="comment-form"> <textarea class="comment-textarea"></textarea> <button class="comment-submit">提交</button> </div> </div> ``` CSS: ```css .comments { margin-top: 20px; } .comment-list { list-style: none; padding-left: 0; } .comment-header { font-size: 12px; color: #666; margin-bottom: 5px; } .comment-author { font-weight: bold; } .comment-time { margin-left: 10px; } .children { margin-left: 20px; } .comment-form { margin-top: 20px; } .comment-textarea { width: 100%; height: 80px; margin-bottom: 10px; } .comment-submit { display: block; margin: 0 auto; } ``` JavaScript: ```javascript $(function() { // 展开折叠评论 $('.comments').on('click', '.comment-header', function() { $(this).next('.comment-content').slideToggle(); $(this).next('.children').slideToggle(); }); // 回复评论 $('.comments').on('click', '.comment-reply', function(e) { e.preventDefault(); var $parent = $(this).closest('li'); var $form = $('.comment-form').clone(true, true); $form.addClass('reply-form'); $form.find('.comment-submit').attr('data-parent', $parent.attr('id')); $form.insertAfter($parent.find('.comment-content')); }); // 提交评论 $('.comments').on('click', '.comment-submit', function() { var $textarea = $(this).siblings('.comment-textarea'); var $parent = $(this).closest('li'); var comment = $textarea.val(); var parentId = $(this).data('parent'); if (comment !== '') { var $comment = $('<li>'); $comment.attr('id', 'comment-' + Date.now()); $comment.html(` <div class="comment-header"> <span class="comment-author">匿名:</span> <span class="comment-time">${getCurrentTime()}</span> <a href="#" class="comment-reply">回复</a> </div> <div class="comment-content"> ${comment} </div> `); if (parentId) { var $children = $parent.find('.children'); if ($children.length === 0) { $children = $('<ul class="children">'); $parent.append($children); } $children.append($comment); } else { $('.comment-list').append($comment); } $textarea.val(''); } }); function getCurrentTime() { var date = new Date(); var year = date.getFullYear(); var month = padZero(date.getMonth() + 1); var day = padZero(date.getDate()); var hours = padZero(date.getHours()); var minutes = padZero(date.getMinutes()); var seconds = padZero(date.getSeconds()); return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; } function padZero(num) { return num < 10 ? '0' + num : num; } }); ``` 在这个示例中,我们使用了 jQuery实现评论框的功能: 1. 点击评论头部可以展开折叠评论内容和子评论。 2. 点击回复按钮会在被回复的评论下面添加一个回复表单。 3. 点击提交按钮会将评论或回复添加到相应的位置,并清空评论框。 需要注意的是,这只是一个简单的示例,实际应用中可能会有更多的功能需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值