Javascript 如何从forEach循环中只选择一个项目,而取消选择其他项目?

Javascript 如何从forEach循环中只选择一个项目,而取消选择其他项目?,javascript,html,css,Javascript,Html,Css,我需要在单击此元素时更改元素颜色,在单击另一个元素时仅更改该元素的颜色 const postCategories = document.querySelectorAll('.post-cats-li'); const postCategoriesIcon = document.querySelectorAll('.post-cat-img'); const postCats = document.querySelectorAll('.post-cats'); postCategories.fo

我需要在单击此元素时更改元素颜色,在单击另一个元素时仅更改该元素的颜色

const postCategories = document.querySelectorAll('.post-cats-li');
const postCategoriesIcon = document.querySelectorAll('.post-cat-img');
const postCats = document.querySelectorAll('.post-cats');

postCategories.forEach(function(categories,index){
    categories.addEventListener('click',function(){
        postCategoriesIcon[index].style.filter = "grayscale(0%)";
    });
});


未经测试。。。这将在更改任何内容之前保留对选定图元样式及其默认值的引用

const postCategories = document.querySelectorAll('.post-cats-li')
const postCategoriesIcon = document.querySelectorAll('.post-cat-img')
const postCats = document.querySelectorAll('.post-cats')
let selected // [styleMap, "default filter value"]

postCategories.forEach(function (categories, index) {
  categories.addEventListener('click', function () {
    if (selected) {
      // reset filter if any is selected
      selected[0].filter = selected[1] 
    }
    // store the selected style and (default) filter before changing
    selected = postCategoriesIcon[index].style
    selected = [selected, selected.filter]
    // set style
    selected[0].filter = "grayscale(0%)"
  })
})
通常情况下,您会选择一个
类,并使用css对其进行样式设置,然后按照以下行执行操作:

const postCategories = document.querySelectorAll('.post-cats-li')
const postCategoriesIcon = document.querySelectorAll('.post-cat-img')
const postCats = document.querySelectorAll('.post-cats')

postCategories.forEach(function (categories, index) {
  categories.addEventListener('click', function () {
    // remove the `selected` class from anywhere first
    postCategories.querySelectorAll('.selected').forEach(function (selected) {
      selected.classList.remove('selected')
    })
    // set the current element as selected
    postCategoriesIcon[index].classList.add('selected')
  })
})

未经测试的。。。这将在更改任何内容之前保留对选定图元样式及其默认值的引用

const postCategories = document.querySelectorAll('.post-cats-li')
const postCategoriesIcon = document.querySelectorAll('.post-cat-img')
const postCats = document.querySelectorAll('.post-cats')
let selected // [styleMap, "default filter value"]

postCategories.forEach(function (categories, index) {
  categories.addEventListener('click', function () {
    if (selected) {
      // reset filter if any is selected
      selected[0].filter = selected[1] 
    }
    // store the selected style and (default) filter before changing
    selected = postCategoriesIcon[index].style
    selected = [selected, selected.filter]
    // set style
    selected[0].filter = "grayscale(0%)"
  })
})
通常情况下,您会选择一个
类,并使用css对其进行样式设置,然后按照以下行执行操作:

const postCategories = document.querySelectorAll('.post-cats-li')
const postCategoriesIcon = document.querySelectorAll('.post-cat-img')
const postCats = document.querySelectorAll('.post-cats')

postCategories.forEach(function (categories, index) {
  categories.addEventListener('click', function () {
    // remove the `selected` class from anywhere first
    postCategories.querySelectorAll('.selected').forEach(function (selected) {
      selected.classList.remove('selected')
    })
    // set the current element as selected
    postCategoriesIcon[index].classList.add('selected')
  })
})
以下是我的方法)

const postCategories=document.queryselectoral('.post-cats-li');
const postCategoriesIcon=document.queryselectoral('.post-cat-img');
const postCats=document.queryselectoral('.post-cats');
邮政分类。forEach(功能(类别、索引){
categories.addEventListener('click',function()){
const postCatsLength=postCats.length;
for(设i=0;i
以下是我的方法)

const postCategories=document.queryselectoral('.post-cats-li');
const postCategoriesIcon=document.queryselectoral('.post-cat-img');
const postCats=document.queryselectoral('.post-cats');
邮政分类。forEach(功能(类别、索引){
categories.addEventListener('click',function()){
const postCatsLength=postCats.length;
for(设i=0;i
通常,您必须向按钮添加一个选择类,然后在取消选择按钮时删除选择。你想要一个例子吗?我需要的是图像上显示的内容选择一种颜色,另一种选择另一种颜色,并将其他元素的颜色更改为默认值。请发布你的html。给你一个例子会更快。一般来说,你有两个选择。在内存中保留对当前选定项的引用,然后单击,删除该选择,然后选择新项。选项二,单击时从所有选项中删除选择类,然后将选择类添加到单击的项目。通常,您必须向按钮添加选择类,然后在取消选择按钮时删除选择。你想要一个例子吗?我需要的是图像上显示的内容选择一种颜色,另一种选择另一种颜色,并将其他元素的颜色更改为默认值。请发布你的html。给你一个例子会更快。一般来说,你有两个选择。在内存中保留对当前选定项的引用,然后单击,删除该选择,然后选择新项。选项二,单击时从所有选项中删除选择类,然后将选择类添加到单击的项目中。