简体   繁体   English

当我单击 Javascript 中另一个按钮的内容时,如何关闭一个按钮内容

[英]How do i make one button content close when i click another button's content in Javascript

My problem is that I have a website, using javascript I have made it when I click on the About Me it opens, but when I click on Education and Achievements the About me button's content remains open and the Education stuff overlaps it.我的问题是我有一个网站,使用 javascript 当我点击关于我时它打开了,但是当我点击教育和成就时,关于我按钮的内容保持打开并且教育内容重叠它。 I want to make it so that when I click on another button the first one closes (its content. The website name is dirieahmed.ml我想这样做,以便当我单击另一个按钮时,第一个按钮会关闭(它的内容。网站名称是 dirieahmed.ml

My code HTML and JS will be linked below ill add CSS later if need be.我的代码 HTML 和 JS 将在下面链接,如果需要,稍后会添加 CSS。

<div class="container">
  <button id="one" class="click one">About Me</button>
  <div class="list-1 sec">
    <h1 class="content-header-one content">Dummy text header</h1>
    <p class="content-paragraph-one">Dummy text</p>
  </div>
  <button class="click two">Education and Achivements</button>
  <div class="list-2 sec">
    <p class="content-paragraph2 content">Dummy text</p>
    <ul class="content-list content">
      <li>- Achivement #1</li>
      <li>- Achivement #2</li>
      <li>- Achivement #3</li>
    </ul>
  </div>
  <button class="click three" >Other</button>
  <div class="list-3 sec">
    <h1 class="content-header-two content">Dummy text header</h1>
    <p class="content-paragraph3 content">Dummy text</p>
  </div>
  <script async>
    let one = document.querySelector('.one');
    let two = document.querySelector('.two');
    let three = document.querySelector('.three');
    let list1 = document.querySelector('.list-1');
    let list2 = document.querySelector('.list-2');
    let list3 = document.querySelector('.list-3');
      one.addEventListener("click", ()=>{
        list1.classList.toggle('newlist');  
    });
    two.addEventListener("click", ()=>{
        list2.classList.toggle('newlist');
        lis
    })
    three.addEventListener("click", ()=>{
        list3.classList.toggle('newlist')
    });
 
    // you gotta chnage this
// change above
  </script>
</div> 

The document object should be given a click event listener.文档object 应该被赋予一个点击事件监听器。 Use the contains() function to determine with each click if the clicked element is outside the targeted element.使用contains() function 来确定每次点击时点击的元素是否在目标元素之外。 Hide the original element if the clicked element is outside.如果单击的元素在外部,则隐藏原始元素。

To summarize you will need to hide all other ones when there is a click anywhere and then only show the ones based on the link that you clicked总而言之,您需要在任何地方单击时隐藏所有其他链接,然后仅显示基于您单击的链接的链接

Check the code below检查下面的代码

https://codesandbox.io/s/keen-driscoll-igzlwb?file=/index.html:2333-3356 https://codesandbox.io/s/keen-driscoll-igzlwb?file=/index.html:2333-3356

     <script async>
        // Instanciate List div's
        let list1 = document.querySelector(".list-1");
        let list2 = document.querySelector(".list-2");
        let list3 = document.querySelector(".list-3");

        // Reset's all views to the default without .newlist
        const resetAllViews = () => {
          list1.classList.remove("newlist");
          list2.classList.remove("newlist");
          list3.classList.remove("newlist");
        };

        // Checks to see what button is clicked and shows correct list based on input
      document.addEventListener(
        "click",
        function (e) {
          e = e || window.event;
          var target = e.target;
          if (target.classList.contains("one")) {
            resetAllViews();
            list1.classList.add("newlist");
          }
          if (target.classList.contains("two")) {
            resetAllViews();
            list2.classList.add("newlist");
          }
          if (target.classList.contains("three")) {
            resetAllViews();
            list3.classList.add("newlist");
          }
        }, false);

      </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让屏幕上的所有内容都变暗,除了你点击汉堡按钮的 sidenav 一个 - How do I make all content on screen go dark except the sidenav one you click on the burger button 单击按钮显示内容时如何隐藏按钮? - how to hide button when i click the button to show content? jQuery:单击删除内容后,如何使用触发添加功能的按钮重置按钮? - Jquery: After removing content with a click how do I reset the button with the one I triggered the add function? 当我单击另一个功能时,如何使按钮单击事件发生? - How do i make a button click event happen when i click on another function? 单击按钮(包括内容)时,如何用textarea替换div? - How do I replace div with textarea when I click button (including content)? 如何在单击按钮时更改div的文本内容? 香草JS / jQuery - How do I change a div's text content on button click? Vanilla JS/Jquery 如何使JQuery关闭按钮? - How do I make a JQuery close a button? 当我使用javascript单击按钮时,如何在另一页上显示特定图像? - How to make a specific image display on another page when I click on a button using javascript? 如果不更改宽度而更改内容时如何防止按钮缩小? - How do I prevent a button from shrinking when it's content changes without settings a width? 当我使用jQuery单击另一个下拉菜单按钮时,如何关闭活动的下拉菜单? - How can I close an active dropdown menu when I click on another dropdown menu button using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM