You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何仅删除选中的用户输入而非整个HTML块?

如何仅删除选中的用户输入而非整个HTML块?

嘿,我看了你代码里的问题啦——核心问题出在重复使用相同ID,还有事件绑定的逻辑没理顺。HTML里的ID必须是独一无二的,你每次新建笔记都给删除按钮用同一个ID,浏览器只会找到第一个匹配的元素,自然删不对;而且你代码里还重复创建了两个删除按钮(一个用createElement做的noteDelete,一个写在innerHTML里的deleteNote),这也导致逻辑混乱了。

我给你两种解决方案,先讲最直观的,再讲更高效的事件委托方式:


方案一:给每个笔记单独绑定删除事件

这种方式逻辑简单,适合刚上手的情况,每创建一个笔记就给它的删除按钮绑定专属事件:

// 先获取所有需要的元素
const titleArea = document.getElementById('title');
const noteArea = document.getElementById('note');
const submitNoteButton = document.getElementById('addNote');
const noteSum = document.getElementById('noteValue');

submitNoteButton.addEventListener('click', () => {
  const listTitle = titleArea.value.trim();
  const noteInput = noteArea.value.trim();

  // 先判断标题是否为空,避免创建空笔记
  if (!listTitle) return;

  // 创建整个笔记的容器,把标题、内容、按钮都装进去
  const noteContainer = document.createElement('div');
  noteContainer.classList.add('note-item'); // 加个类方便后续操作

  noteContainer.innerHTML = `
    <h3>${listTitle}</h3>
    <p>${noteInput}</p>
    <button type="button" class="delete-note-btn">Delete</button>
    <button type="button" class="view-note-btn">View</button>
  `;

  // 把笔记容器添加到页面上
  noteSum.appendChild(noteContainer);

  // 清空输入框
  noteArea.value = "";
  titleArea.value = "";

  // 给当前笔记的删除按钮绑定事件
  const deleteBtn = noteContainer.querySelector('.delete-note-btn');
  deleteBtn.addEventListener('click', () => {
    // 直接删除整个笔记容器,连带里面的所有内容
    noteContainer.remove();
  });

  // 顺便给查看按钮加个简单逻辑,你可以改成自己想要的效果
  const viewBtn = noteContainer.querySelector('.view-note-btn');
  viewBtn.addEventListener('click', () => {
    alert(`标题:${listTitle}\n内容:${noteInput}`);
  });
});

改动说明:

  • 给每个笔记创建一个独立的容器noteContainer,把标题、内容、按钮都放在里面,这样删除的时候直接删掉这个容器就不会影响其他笔记。
  • 类名(比如delete-note-btn)代替重复的ID,类可以重复使用,而且通过容器查找自己的按钮,不会找错。
  • 每次创建笔记后,只给当前笔记的删除按钮绑定事件,点击哪个按钮就删对应的笔记。

方案二:用事件委托(更高效)

如果以后要创建很多笔记,每次绑定事件会有点繁琐,用事件委托只需要给父容器绑定一次事件,就能处理所有子按钮的点击:

// 先获取所有需要的元素
const titleArea = document.getElementById('title');
const noteArea = document.getElementById('note');
const submitNoteButton = document.getElementById('addNote');
const noteSum = document.getElementById('noteValue');

// 给父容器绑定一次事件,处理所有删除和查看按钮的点击
noteSum.addEventListener('click', (e) => {
  // 判断点击的是不是删除按钮
  if (e.target.classList.contains('delete-note-btn')) {
    // 找到按钮所在的笔记容器并删除
    e.target.closest('.note-item').remove();
  }

  // 判断点击的是不是查看按钮
  if (e.target.classList.contains('view-note-btn')) {
    const noteItem = e.target.closest('.note-item');
    const title = noteItem.querySelector('h3').textContent;
    const content = noteItem.querySelector('p').textContent;
    alert(`标题:${title}\n内容:${content}`);
  }
});

// 添加笔记的逻辑,不用再给按钮绑定事件了
submitNoteButton.addEventListener('click', () => {
  const listTitle = titleArea.value.trim();
  const noteInput = noteArea.value.trim();

  if (!listTitle) return;

  const noteContainer = document.createElement('div');
  noteContainer.classList.add('note-item');

  noteContainer.innerHTML = `
    <h3>${listTitle}</h3>
    <p>${noteInput}</p>
    <button type="button" class="delete-note-btn">Delete</button>
    <button type="button" class="view-note-btn">View</button>
  `;

  noteSum.appendChild(noteContainer);

  // 清空输入框
  noteArea.value = "";
  titleArea.value = "";
});

事件委托的好处:

不管后续创建多少笔记,都不用再重复绑定事件,父容器会监听所有子元素的点击,判断是不是目标按钮再执行对应操作,代码更简洁,性能也更好。


你的HTML部分不用大改,保持原来的结构就行,只要确保没有重复ID就没问题啦。

备注:内容来源于stack exchange,提问作者LaMonte Shepard

火山引擎 最新活动