本文详解如何通过原生 javascript 实现点击不同按钮时,将同一表单(如 `#answer-form`)动态插入到对应目标位置(如注释标记 `` 所在的父容器),并支持显隐切换与位置重定位。
在实际开发中,常需复用一个表单(如评论回复框)并根据用户操作将其“移动”至不同上下文区域——例如在多条评论下分别点击“回复”按钮,使同一个 #answer-form 出现在对应评论下方。注意:DOM 中的 appendChild() 并非复制元素,而是迁移节点,因此无需手动移除旧位置,直接追加即自动解绑原父节点。
以下为完整、健壮的实现方案:
评论 1 内容... 评论 2 内容...لطفا پاسخ خود را بنویسید
// 获取表单与所有回复按钮
const answerForm = document.getElementById('answer-form');
const answerButtons = document.querySelectorAll('.answer-btn');
// 点击回复按钮:移动并显示表单
answerButtons.forEach(btn => {
btn.addEventListener('click', function () {
const targetId = this.getAttribute('data-target');
const targetSection = document.querySelector(targetId);
// 若表单已显示,先隐藏并从当前父节点移除(可选:保留状态)
if (answerForm.style.display === 'block') {
answerForm.style.display = 'none';
// 可选:清除表单内容以重置
answerForm.querySelector('textarea#Text').value = '';
answerForm.querySelector('input#BookFK').value = '';
return;
}
// 移动表单到目标区域末尾(即注释标记所在位置之后)
if (targetSection) {
// 清空目标区可能残留的旧表单(防御性处理)
const existingForm = targetSection.querySelector('#answer-form');
if (existingForm) existingForm.remove();
// 插入到目标容器内最后(即紧邻 `` 的位置)
targetSection.appendChild(answerForm);
answerForm.style.display = 'block';
}
});
});
// 独立隐藏函数(供取消按钮调用)
function hideAnswerForm() {
answerForm.style.display = 'none';
}
。该方案不依赖 jQuery 或任何框架,纯原生 JavaScript 实现,语义清晰、易于维护,并完美满足“点击切换位置+显隐”的核心需求。