0 ? 'style="margin-left: ' + indent + 'px;"' : ''; // 创建评论HTML var commentHtml = ` 昵称:${comment.name || '匿名用户'} ${(comment.title || '').replace(/\r\n/g, '')} 时间:${comment.date || '未知'} 发布+ `; // 创建临时div var tempDiv = document.createElement('div'); tempDiv.innerHTML = commentHtml; fragment.appendChild(tempDiv.firstElementChild); }); // 一次性添加到容器 container.append(fragment); // 单独处理子评论渲染,避免使用:last-child var childContainers = container.find('.child-comments'); comments.forEach(function (comment, index) { var $childContainer = $(childContainers[index]); if (comment.list && comment.list.length > 0) { // 使用setTimeout避免递归阻塞 setTimeout(function() { renderComments(comment.list, depth + 1, $childContainer); }, 0); } }); } // 页面加载完成后渲染评论 $(document).ready(function () { console.log('开始渲染评论...'); // 确保容器存在 if ($('#commentContainer').length === 0) { console.error('找不到评论容器 #commentContainer'); return; } // 清空容器 $('#commentContainer').empty(); // 渲染评论 renderComments(pinglunData); console.log('评论渲染完成'); }); // 添加评论折叠/展开功能 $(document).on('click', '.c-name', function (e) { e.stopPropagation(); var $item = $(this).closest('.c-item'); var $childComments = $item.find('.child-comments'); $childComments.toggle(); }); function pinglun(e) { var $form = $(e); var $btn = $form.find('.comm-rel-btn'); // 防止重复提交 if ($btn.prop('disabled')) { return false; } // 禁用按钮 $btn.prop('disabled', true).text('提交中...'); var d = {}; var t = $(e).serializeArray(); $.each(t, function() { d[this.name] = this.value; }); d['type'] = 0; d['id'] = 14720; // 添加超时处理 $.ajax({ url: '/index/index/pinglun.html', type: 'POST', data: d, dataType: 'json', timeout: 10000, // 10秒超时 success: function (re) { if (re.code == 1) { alert(re.msg); // 延迟刷新,避免立即刷新导致用户看不到提示 setTimeout(function() { window.location.reload(); }, 500); } else { alert(re.msg); } }, error: function(xhr, status, error) { if (status === 'timeout') { alert('请求超时,请重试'); } else { alert('网络错误: ' + error); } }, complete: function() { // 恢复按钮状态 setTimeout(function() { $btn.prop('disabled', false).text('发布+'); }, 1000); } }); return false; }