微信小程序 内容评论-回复评论-回复回复的实现

本文介绍了如何在微信小程序中实现内容评论、回复评论以及回复回复的功能。通过展示wxml、wxss和js代码段,详细讲解了输入框失去焦点时的逻辑处理以及顶部和底部输入框的操作相似性。
摘要由CSDN通过智能技术生成

效果图

在这里插入图片描述

先展示代码js逻辑有详细注释

代码格式有点乱复制粘贴格式化即可

wxml

<!-- 评论-回复-回复评论显示区域 -->  
<view class="container">
    <!-- 总共评论数 -->    
    <view class="total">共{
  {comment_list.length + comment_list2.length}}条评论</view>    
    <!-- END -->
    <!-- 评论框 -->    
    <input confirm-type="send" class="container_inp" value="{
    {value}}" placeholder="{
    { placeholder2 }}" placeholder-class="container_place" bindconfirm="bindconfirm"></input>    
    <!-- END -->
    <!-- 用户评论回复显示区域 -->    
    <view class="container-item" wx:for="{
    {comment_list}}" wx:for-item="list" wx:key="key">
      <image class="item_img" src="{
    {list.comment_user_avatar}}"></image>
      <view class="item_right">
        <view class="right_name">{
  {list.comment_user_name}}</view>
        <view class="right_content">
          <text class="right_content_txt" bindtap='replyComment' data-type="1" data-name='{
    {list.comment_user_name}}' data-cid='{
    {list.comment_id}}' data-pid="{
    {list.parent_id}}">{
  {list.comment_text}}</text>
          <text class="right_content_txt2">{
  {list.comment_time}}</text>
          <!-- 回复评论 -->
          <!-- 判断回复列表数据中的parent_id和评论列表数据的comment_id是否相等 相等就显示 不等就不显示 -->          
          <view class="reply" wx:for="{
    {comment_list2}}" wx:for-item="list2" wx:key="list2" wx:if="{
    {list2.parent_id == list.comment_id}}">
            <image class="reply_img" src="{
    {list2.comment_user_avatar}}"></image>
            <view class="reply_right">
              <view class="right_name">{
  {list2.comment_user_name}}</view>
              <text wx:if="{
    {list2.reply_name == ''}}" class="right_content_txt" bindtap='replyComment' data-type="2" data-name='{
    {list2.comment_user_name}}' data-cid='{
    {list2.comment_id}}' data-pid="{
    {list2.parent_id}}">{
  {list2.comment_text}}</text>
              <text wx:if="{
    {list2.reply_name != ''}}" bindtap='replyComment' data-type="2" data-name='{
    {list2.comment_user_name}}' data-cid='{
    {list2.comment_id}}' data-pid
  • 60
    点赞
  • 291
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的微信小程序朋友圈评论回复的前端代码: ```html <!-- 页面结构 --> <view class="page"> <!-- 朋友圈内容 --> <view class="post"> <image class="avatar" src="{{ post.avatar }}"></image> <view class="post-content"> <view class="post-header"> <text class="nickname">{{ post.nickname }}</text> <text class="timestamp">{{ post.timestamp }}</text> </view> <view class="post-text">{{ post.text }}</view> </view> </view> <!-- 评论列表 --> <view class="comment-list"> <view class="comment" wx:for="{{ comments }}"> <image class="avatar" src="{{ item.avatar }}"></image> <view class="comment-content"> <view class="comment-header"> <text class="nickname">{{ item.nickname }}</text> <text class="timestamp">{{ item.timestamp }}</text> </view> <view class="comment-text">{{ item.text }}</view> </view> <!-- 回复列表 --> <view class="reply-list"> <view class="reply" wx:for="{{ item.replies }}"> <image class="avatar" src="{{ item.avatar }}"></image> <view class="reply-content"> <view class="reply-header"> <text class="nickname">{{ item.nickname }}</text> <text class="timestamp">{{ item.timestamp }}</text> </view> <view class="reply-text">{{ item.text }}</view> </view> </view> </view> <!-- 回复输入框 --> <view class="reply-input"> <input class="input" placeholder="回复评论" bindinput="onReplyInput"></input> <button class="button" bindtap="onReplySubmit">发送</button> </view> </view> </view> </view> ``` ```javascript // 页面逻辑 Page({ data: { post: { avatar: 'https://example.com/avatar.jpg', nickname: '小明', timestamp: '2022-01-01 12:00:00', text: '今天天气真好!' }, comments: [ { avatar: 'https://example.com/avatar.jpg', nickname: '小红', timestamp: '2022-01-01 12:10:00', text: '是啊,阳光明媚!', replies: [] }, { avatar: 'https://example.com/avatar.jpg', nickname: '小李', timestamp: '2022-01-01 12:20:00', text: '好想出去玩啊!', replies: [ { avatar: 'https://example.com/avatar.jpg', nickname: '小张', timestamp: '2022-01-01 12:30:00', text: '我们一起去吧!' } ] } ], replyCommentIndex: -1, replyInputValue: '' }, // 监听回复输入框内容 onReplyInput(event) { this.setData({ replyInputValue: event.detail.value }) }, // 提交回复 onReplySubmit() { const { replyCommentIndex, replyInputValue } = this.data if (replyInputValue.trim().length === 0) { wx.showToast({ icon: 'none', title: '回复内容不能为空' }) return } const reply = { avatar: 'https://example.com/avatar.jpg', nickname: '我', timestamp: new Date().toLocaleString(), text: replyInputValue } if (replyCommentIndex === -1) { // 回复评论 const comments = this.data.comments comments.push({ avatar: 'https://example.com/avatar.jpg', nickname: '我', timestamp: new Date().toLocaleString(), text: replyInputValue, replies: [] }) this.setData({ comments, replyInputValue: '' }) } else { // 回复回复 const comments = this.data.comments const replies = comments[replyCommentIndex].replies replies.push(reply) comments[replyCommentIndex].replies = replies this.setData({ comments, replyCommentIndex: -1, replyInputValue: '' }) } }, // 点击评论回复按钮 onCommentReply(event) { const commentIndex = event.currentTarget.dataset.index this.setData({ replyCommentIndex: commentIndex }) } }) ``` 在这个代码中,我们首先定义了一个页面结构,包含一个朋友圈内容和一个评论列表。评论列表中包含多个评论,每个评论下面还有一个回复列表和一个回复输入框。我们使用了`wx:for`指令来渲染列表数据。 在页面逻辑中,我们监听了回复输入框的内容变化和回复按钮的点击事件。当用户输入回复内容并点击发送按钮时,我们会根据当前的界面状态来判断是回复评论还是回复回复,并将新的回复添加到对应的评论回复列表中。同时,我们还对空内容做了校验,并在用户输入空内容时弹出提示框。 最后,我们还监听了评论回复按钮的点击事件,并将当前回复评论索引保存在`replyCommentIndex`变量中。这个变量的值为-1时表示用户正在回复评论而不是回复回复

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值