如何将getElementById或QuerySelectorall用于无限数量的ID? (创建切换按钮以显示/隐藏Divs)

发布于 2025-02-09 09:58:48 字数 1667 浏览 2 评论 0原文

您可能会说,JavaScript Noob在这里,想知道是否有人可以帮助我清理此操作。请避免jQuery。

这是非常基本的:在该部分中有一个切换按钮,一部分内容和一个关闭按钮。默认情况下隐藏了内容,但是当单击“切换”按钮时,内容将变得可见,并且用户会自动滚动。单击“关闭”按钮时,内容部分消失了,并将用户自动滚动回到主部分,并使用所有切换按钮。

对于内容的各个部分,应重复此操作,每个独特的内容部分与其相应的切换按钮相关联。

jsfiddle: https://jsfiddle.net.net/bythrees/bythrees/bythrees/bythrees/mw5nh1cd/mw5nh1cd/51cd/51/51/

>这是按预期的工作,但是有一种方法可以清理JavaScript,以便如果需要超过2个内容,它将自动发生,而无需添加另一个部分 - 切换3,切换4,切换5等 - 致JS?我正在研究QuerySelectorall(我不必使用ID,可以轻松使用类)和映射,但不知道如何使其工作。

这是我正在使用的代码。如果我必须为每个切换重复所有这些,则似乎很愚蠢,更改每个切换的数字。

document.addEventListener('DOMContentLoaded', function() {

  const toggle1 = document.getElementById('toggle-1');
  const toggleContent1 = document.getElementById('toggle-content-1');
  const closeContent1 = document.getElementById('close-toggle-1');

  const toggle2 = document.getElementById('toggle-2');
  const toggleContent2 = document.getElementById('toggle-content-2');
  const closeContent2 = document.getElementById('close-toggle-2');

  // Toggle 1

  if (toggle1) {
    toggle1.addEventListener('click', () => {
      toggleContent1.classList.toggle('show');
    });

    closeContent1.addEventListener('click', () => {
      toggleContent1.classList.remove('show');
    });
  }

  // Toggle 2

  if (toggle2) {
    toggle2.addEventListener('click', () => {
      toggleContent2.classList.toggle('show');
    });

    closeContent2.addEventListener('click', () => {
      toggleContent2.classList.remove('show');
    });
  }

});

提前致谢!

Javascript noob here, as you can probably tell, and was wondering if anyone could help me clean this up. Trying to avoid Jquery, please.

It’s pretty basic: There’s a toggle button, a section of content, and a close button within that section of content. The content is hidden by default, but when the toggle button is clicked, the content becomes visible and the user gets auto-scrolled to it. When the close button is clicked, the section of content disappears and the user is auto-scrolled back up to the main section with all the toggle buttons.

This should be repeated for individual sections of content, with each unique content section associated with its corresponding toggle button.

JSFiddle: https://jsfiddle.net/bythrees/mw5nh1cd/51/

This works as intended but is there a way to clean up the Javascript so that if more than 2 sections of content are desired, it will happen automatically, without having to add another section — Toggle 3, Toggle 4, Toggle 5, and so on — to the JS? I was looking into querySelectorAll (I don't HAVE to be using IDs, can easily use classes) and map but don’t know how to make it work.

Here's the code I'm using. It just seems dumb if I have to repeat all of that for each Toggle, changing the numbers for each one.

document.addEventListener('DOMContentLoaded', function() {

  const toggle1 = document.getElementById('toggle-1');
  const toggleContent1 = document.getElementById('toggle-content-1');
  const closeContent1 = document.getElementById('close-toggle-1');

  const toggle2 = document.getElementById('toggle-2');
  const toggleContent2 = document.getElementById('toggle-content-2');
  const closeContent2 = document.getElementById('close-toggle-2');

  // Toggle 1

  if (toggle1) {
    toggle1.addEventListener('click', () => {
      toggleContent1.classList.toggle('show');
    });

    closeContent1.addEventListener('click', () => {
      toggleContent1.classList.remove('show');
    });
  }

  // Toggle 2

  if (toggle2) {
    toggle2.addEventListener('click', () => {
      toggleContent2.classList.toggle('show');
    });

    closeContent2.addEventListener('click', () => {
      toggleContent2.classList.remove('show');
    });
  }

});

Thanks in advance!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文