简体   繁体   English

如何制作一个点击下拉的下拉列表?

[英]How to make one droplist which is clicked to drop?

just few month ago started learning programming.就在几个月前开始学习编程。 At the moment studying JS and want to make dropable droplist.目前正在学习 JS 并想制作 dropable droplist。 Created few droplists and occur with the problem, what all dropdown lists opening after click.创建了几个下拉列表并出现问题,点击后所有下拉列表打开。 Need to be opened only one which was clicked:只需要打开一个被点击的:

<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.css" rel="stylesheet"  type='text/css'>
<div class="main">
  <div class="droplist__box">
    <div class="droplist__selected">
      <span>Choose options</span>
      <i class="fa fa-chevron-down"></i>
    </div>
    <div class="droplist__items-container">
      <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 1</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 2</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 3</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 4</span> 
        </label>
      </div>
    </div>
  </div>

  <div class="droplist__box">
    <div class="droplist__selected">
      <span>Choose options</span>
      <i class="fa fa-chevron-down"></i>
    </div>
    <div class="droplist__items-container">
      <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 1</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 2</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 3</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 4</span> 
        </label>
      </div>
    </div>
  </div>

  <div class="droplist__box">
    <div class="droplist__selected">
      <span>Choose options</span>
      <i class="fa fa-chevron-down"></i>
    </div>
    <div class="droplist__items-container">
      <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 1</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 2</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 3</span> 
        </label>
      </div>
          <div class="droplist__item">
        <label for=""> 
          <input type="checkbox" name="" id=""> 
          <span>Option 4</span> 
        </label>
      </div>
    </div>
  </div>
</div>

And write simple JS vanilla code:并编写简单的 JS vanilla 代码:

const droplistLabel = document.querySelectorAll('.droplist__selected');
const droplist = document.querySelectorAll('.droplist__items-container');

for (i = 0; i < droplistLabel.length; i ++){
  droplistLabel[i].addEventListener('click', () => {
    for (i = 0; i < droplist.length; i ++){
      droplist[i].classList.toggle('display')
    };
  });
}

Can somebody tell me where i made a mistake?有人能告诉我我在哪里做错了吗?

https://codepen.io/miroso/pen/dyGXOYq https://codepen.io/miroso/pen/dyGXOYq

Thanks for helping me, want to ask extra question: Also, maybe could you suggest solution if for example click is made outside the div, what condition i should write in the code?感谢您帮助我,想问额外的问题:另外,如果例如在 div 之外进行单击,您能否提出解决方案,我应该在代码中写什么条件?

You are doing toggle to all the containers... you should toggle only the one that corresponds to clicked element... so in you javascript you should take care of that, if you change your javascript for:您正在切换所有容器...您应该只切换与单击的元素相对应的那个...所以在 javascript 中,如果您将 javascript 更改为:

    const droplistLabel = document.querySelectorAll('.droplist__selected');

    for (i = 0; i < droplistLabel.length; i ++){
      droplistLabel[i].addEventListener('click', (e) => {
        let parent, droplist;

        // look for droplist__selected
        parent=e.srcElement;
        while(!parent.classList.contains('droplist__selected')) {
          parent=parent.parentElement;
        }

        // get container of list and toggle
        droplist = parent.parentElement.querySelector('.droplist__items-container');
        droplist.classList.toggle('display');
      });
    }

As you can see, first get parent of clicked element that has class droplist__selected , then toggle list of parent container... and that's all.如您所见,首先获取具有 class droplist__selected的单击元素的父元素,然后切换父容器列表...仅此而已。

Edit 1编辑 1

Maybe this is more complicated... you have to:也许这更复杂......你必须:

  1. Add event to document click to close all.将事件添加到文档单击以关闭所有。
  2. On an item click close all but clicked one.在一个项目上单击关闭所有但单击一个。
  3. Toggle clicked item.切换单击的项目。

So:所以:

    const droplistLabel = document.querySelectorAll('.droplist__selected');

  document.addEventListener('click', (e) => {
        // close all...
        droplist = document.querySelectorAll('.droplist__items-container');
        for(j=0;j<droplist.length;j++) {
          droplist[j].classList.remove('display');
        }  
  });

    for (i = 0; i < droplistLabel.length; i ++){
      droplistLabel[i].addEventListener('click', (e) => {
        let parent, droplist, droplists;

        // Cancel event to avoid document.click reached... 
        e.stopPropagation();

        // look for droplist__selected
        parent=e.srcElement;
        while(!parent.classList.contains('droplist__selected')) {
          parent=parent.parentElement;
        }

        // close all but clicked, get clicked and compare with the others...
        droplist = parent.parentElement.querySelector('.droplist__items-container');

        droplists = document.querySelectorAll('.droplist__items-container');
        for(let j=0;j<droplists.length;j++) {
          if(droplists[j]!=droplist) {
            droplists[j].classList.remove('display');
          }
        }       

        // finally toggle clicked... get container of list and toggle
        droplist.classList.toggle('display');
      });
    }

Hope it helps!!!希望能帮助到你!!!

Change your javascript code to将您的 javascript 代码更改为

Step 1: toggle the display clicked element, if already in shown state then hide and vice-versa第 1 步:切换显示点击的元素,如果已经显示 state 然后隐藏,反之亦然

Step 2: for all the other element except the clicked item - remove the class to hide them第 2 步:对于除单击项目之外的所有其他元素 - 删除 class 以隐藏它们

const droplistLabel = document.querySelectorAll('.droplist__selected');
const droplist = document.querySelectorAll('.droplist__items-container');

for (let i = 0; i < droplistLabel.length; i ++){
  droplistLabel[i].addEventListener('click', () => {
      droplist[i].classList.toggle('display');**// toggle the display clicked element, if already in shown state then hide and vice-versa**
      for(let j=0;j<droplist.length;j++)
      {
          if(i!=j)**//for all the other element except the clicked item - remove the class to hide them**
           droplist[j].classList.remove('display');
      } 
  });
}

暂无
暂无

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

相关问题 2个带有2个提交按钮的下拉框,单击了哪个? - 2 drop down boxes with 2 submit buttons, which one was clicked? 下拉菜单会打开我 map 的每个项目,我如何才能只在我点击的项目上打开 - drop down opens up on every Item I map through How can I make open up only on the one I clicked 如何识别单击了 Slickgrid 中一个单元格中的两个按钮中的哪一个 - How to recognize which of two buttons in one cell in Slickgrid was clicked 在React中如何显示所有被单击的div,如何隐藏所有div? - How to hide all the div while showing the one which is clicked, in React? 如何使用.not()隐藏除单击的div外的所有div - How to use .not() to hide all divs except the one which was clicked JavaScript如何关闭另一个下拉菜单时的下拉菜单。 没有jQuery - JavaScript How to close one drop down when another one clicked to open. No Jquery 我如何知道单击了哪个HTML表的列下拉列表 - How do i get to Know which HTML table Column Drop-down is clicked 如果单击另一个下拉菜单,如何自动关闭下拉菜单? - How can I make a dropdown close automatically if an other one is clicked? 如何使复选框在 24 中仅单击一次? - how to make checkbox clicked just one time in 24? jQuery下拉菜单。 单击后,子菜单消失。 那么,如何在单击时使子菜单停留? - Jquery Drop Down Menu. The sub menu disappears when clicked. So, How to make the submenu stays when clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM