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

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

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.

colorItems = document.querySelector('#color').children[1]

colorItems.children[0].addEventListener('click', () => {
    colorItems.children[0].classList.toggle('on')
    colorItems.children[1].classList.remove('on')
    colorItems.children[2].classList.remove('on')
    colorItems.children[3].classList.remove('on')
})
colorItems.children[1].addEventListener('click', () => {
    colorItems.children[0].classList.remove('on')
    colorItems.children[1].classList.toggle('on')
    colorItems.children[2].classList.remove('on')
    colorItems.children[3].classList.remove('on')
})
colorItems.children[2].addEventListener('click', () => {
    colorItems.children[0].classList.remove('on')
    colorItems.children[1].classList.remove('on')
    colorItems.children[2].classList.toggle('on')
    colorItems.children[3].classList.remove('on')
})
colorItems.children[3].addEventListener('click', () => {
    colorItems.children[0].classList.remove('on')
    colorItems.children[1].classList.remove('on')
    colorItems.children[2].classList.remove('on')
    colorItems.children[3].classList.toggle('on')
})
英文:

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.

colorItems = document.querySelector('#color').children[1]

colorItems.children[0].addEventListener('click', () => {
    colorItems.children[0].classList.toggle('on')
    colorItems.children[1].classList.remove('on')
    colorItems.children[2].classList.remove('on')
    colorItems.children[3].classList.remove('on')
})
colorItems.children[1].addEventListener('click', () => {
    colorItems.children[0].classList.remove('on')
    colorItems.children[1].classList.toggle('on')
    colorItems.children[2].classList.remove('on')
    colorItems.children[3].classList.remove('on')
})
colorItems.children[2].addEventListener('click', () => {
    colorItems.children[0].classList.remove('on')
    colorItems.children[1].classList.remove('on')
    colorItems.children[2].classList.toggle('on')
    colorItems.children[3].classList.remove('on')
})
colorItems.children[3].addEventListener('click', () => {
    colorItems.children[0].classList.remove('on')
    colorItems.children[1].classList.remove('on')
    colorItems.children[2].classList.remove('on')
    colorItems.children[3].classList.toggle('on')
})

答案1

得分: 1

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

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

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

<!-- language: lang-js -->
let colorItems = document.querySelectorAll('#color button');

colorItems.forEach(item => {
    item.addEventListener('click', () => {
        colorItems.forEach(i => i != item ? i.classList.remove('on') : i.classList.add('on'));
    });
});

<!-- language: lang-css -->
.on {
    background-color: yellow;
}

<!-- language: lang-html -->
<div id="color">
    <button>red</button>
    <button>blue</button>
    <button>white</button>
    <button>black</button>
</div>

<!-- 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 -->

let colorItems = document.querySelectorAll(&#39;#color button&#39;);

colorItems.forEach(item =&gt; {
    item.addEventListener(&#39;click&#39;, ()=&gt; {
        colorItems.forEach(i =&gt; i != item ? i.classList.remove(&#39;on&#39;) : i.classList.add(&#39;on&#39;));
    });
});

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

.on {
    background-color: yellow;
}

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

&lt;div id=&quot;color&quot;&gt;
    &lt;button&gt;red&lt;/button&gt;
    &lt;button&gt;blue&lt;/button&gt;
    &lt;button&gt;white&lt;/button&gt;
    &lt;button&gt;black&lt;/button&gt;
&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:

确定