I want to simplify this javascript code. There are many children elements.

huangapple go评论98阅读模式
英文:

I want to simplify this javascript code. There are many children elements

问题

I was trying to make this menu,

when clicked first item,
menu pic 1

when clicked second item,
menu pic 2

i have created styles for class 'on' in style sheet.

  1. colorItems = document.querySelector('#color').children[1]
  2. colorItems.children[0].addEventListener('click', () => {
  3. colorItems.children[0].classList.toggle('on')
  4. colorItems.children[1].classList.remove('on')
  5. colorItems.children[2].classList.remove('on')
  6. colorItems.children[3].classList.remove('on')
  7. })
  8. colorItems.children[1].addEventListener('click', () => {
  9. colorItems.children[0].classList.remove('on')
  10. colorItems.children[1].classList.toggle('on')
  11. colorItems.children[2].classList.remove('on')
  12. colorItems.children[3].classList.remove('on')
  13. })
  14. colorItems.children[2].addEventListener('click', () => {
  15. colorItems.children[0].classList.remove('on')
  16. colorItems.children[1].classList.remove('on')
  17. colorItems.children[2].classList.toggle('on')
  18. colorItems.children[3].classList.remove('on')
  19. })
  20. colorItems.children[3].addEventListener('click', () => {
  21. colorItems.children[0].classList.remove('on')
  22. colorItems.children[1].classList.remove('on')
  23. colorItems.children[2].classList.remove('on')
  24. colorItems.children[3].classList.toggle('on')
  25. })
英文:

I was trying to make this menu,

when clicked first item,
menu pic 1

when clicked second item,
menu pic 2

i have created styles for class 'on' in style sheet.

  1. colorItems = document.querySelector('#color').children[1]
  2. colorItems.children[0].addEventListener('click', () => {
  3. colorItems.children[0].classList.toggle('on')
  4. colorItems.children[1].classList.remove('on')
  5. colorItems.children[2].classList.remove('on')
  6. colorItems.children[3].classList.remove('on')
  7. })
  8. colorItems.children[1].addEventListener('click', () => {
  9. colorItems.children[0].classList.remove('on')
  10. colorItems.children[1].classList.toggle('on')
  11. colorItems.children[2].classList.remove('on')
  12. colorItems.children[3].classList.remove('on')
  13. })
  14. colorItems.children[2].addEventListener('click', () => {
  15. colorItems.children[0].classList.remove('on')
  16. colorItems.children[1].classList.remove('on')
  17. colorItems.children[2].classList.toggle('on')
  18. colorItems.children[3].classList.remove('on')
  19. })
  20. colorItems.children[3].addEventListener('click', () => {
  21. colorItems.children[0].classList.remove('on')
  22. colorItems.children[1].classList.remove('on')
  23. colorItems.children[2].classList.remove('on')
  24. colorItems.children[3].classList.toggle('on')
  25. })

答案1

得分: 1

根据您的代码,这是一个解决方案:

#color 从我看来是您的 容器,它有 子元素,我们假设它们是按钮,您需要使用 querySelectorAll 选择子元素,并循环遍历它们,每当您点击一个 按钮 时,应该向 被点击的按钮 添加 .on CSS 类,并从其 兄弟元素 中移除此类,这里是一个可行的示例:

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-js -->
  3. let colorItems = document.querySelectorAll('#color button');
  4. colorItems.forEach(item => {
  5. item.addEventListener('click', () => {
  6. colorItems.forEach(i => i != item ? i.classList.remove('on') : i.classList.add('on'));
  7. });
  8. });
  9. <!-- language: lang-css -->
  10. .on {
  11. background-color: yellow;
  12. }
  13. <!-- language: lang-html -->
  14. <div id="color">
  15. <button>red</button>
  16. <button>blue</button>
  17. <button>white</button>
  18. <button>black</button>
  19. </div>
  20. <!-- end snippet -->

这段代码将为您实现所需的功能。

英文:

based on your code here is a solution:

#color as i see its your parent container and its has children lets assume they are buttons, you need to select the childrens using querySelectorAll and loop through them, whenever you click on a button you should add .on css class to the clicked button and remove this class from its siblings here is workable example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let colorItems = document.querySelectorAll(&#39;#color button&#39;);
  2. colorItems.forEach(item =&gt; {
  3. item.addEventListener(&#39;click&#39;, ()=&gt; {
  4. colorItems.forEach(i =&gt; i != item ? i.classList.remove(&#39;on&#39;) : i.classList.add(&#39;on&#39;));
  5. });
  6. });

<!-- language: lang-css -->

  1. .on {
  2. background-color: yellow;
  3. }

<!-- language: lang-html -->

  1. &lt;div id=&quot;color&quot;&gt;
  2. &lt;button&gt;red&lt;/button&gt;
  3. &lt;button&gt;blue&lt;/button&gt;
  4. &lt;button&gt;white&lt;/button&gt;
  5. &lt;button&gt;black&lt;/button&gt;
  6. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 16:09:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定