Javascript 将 onClick 从 HTML 传输到 JS

发布于 2025-01-11 10:58:33 字数 1586 浏览 0 评论 0原文

我有一个 HTML 内联 onClick 事件,但我需要将此事件仅传输到 Javascript。我该怎么做?

目前我有

    function renderLinksList() {
      let linksList = ""; // creat an empty variable to manipulate the DOM outside

      for (let i = 0; i < myLinks.length; i++) {
        linksList += `
          <li>
            <a href="${myLinks[i]}" target="_blank">
              ${myLinks[i]}
            </a>
            <button id="remove-btn" onClick="removeIndividualItem(${i})">
              <svg class="close-icon" width="8px" xmlns="http://www.w3.org/2000/svg" 
                viewBox="0 0 320 512">
                <path d="M310.6 361.4c12.5 12.5 12.5 32.75 0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3L54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75 0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-105.4 105.4L310.6 361.4z"/>
               </svg></button>
          </li >
        `;
  }
  listEl.innerHTML = linksList;
}

// remove specific item from localStorage
  function removeIndividualItem(i) {
    myLinks.splice(i, 1);
    localStorage.setItem("myLinks", JSON.stringify(myLinks));
    renderLinksList();
}
    <button id="remove-btn" onClick="removeIndividualItem(${i})">

I have an HTML inline onClick event, but I need to transfer this one to only Javascript. How can I do this?

Currently I have

    function renderLinksList() {
      let linksList = ""; // creat an empty variable to manipulate the DOM outside

      for (let i = 0; i < myLinks.length; i++) {
        linksList += `
          <li>
            <a href="${myLinks[i]}" target="_blank">
              ${myLinks[i]}
            </a>
            <button id="remove-btn" onClick="removeIndividualItem(${i})">
              <svg class="close-icon" width="8px" xmlns="http://www.w3.org/2000/svg" 
                viewBox="0 0 320 512">
                <path d="M310.6 361.4c12.5 12.5 12.5 32.75 0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3L54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75 0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-105.4 105.4L310.6 361.4z"/>
               </svg></button>
          </li >
        `;
  }
  listEl.innerHTML = linksList;
}

// remove specific item from localStorage
  function removeIndividualItem(i) {
    myLinks.splice(i, 1);
    localStorage.setItem("myLinks", JSON.stringify(myLinks));
    renderLinksList();
}
    <button id="remove-btn" onClick="removeIndividualItem(${i})">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2025-01-18 10:58:33

首先抓取元素,然后向其添加事件侦听器:)

const removeBtn = document.getElementById('remove-btn');
removeBtn.addEventListener('click', removeIndividualItem);

然后修改removeIndividualItem(),以便它通过 e.target 定位特定元素
还。您不能有多个具有相同 id 的元素,因此最好将所有按钮定位为特定类。

Start by grabbing element, and then add event listener to it :)

const removeBtn = document.getElementById('remove-btn');
removeBtn.addEventListener('click', removeIndividualItem);

Then modify removeIndividualItem() so it would target specific element by e.target
Also. You cannot have multiple elements with same id, so it might be better to target all buttons with specific class instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文