Javascript 如何将Eventlisteners添加到类的所有元素中,但在鼠标悬停期间仅对特定元素执行它?

Javascript 如何将Eventlisteners添加到类的所有元素中,但在鼠标悬停期间仅对特定元素执行它?,javascript,Javascript,我一直在尝试编写一个javascript代码,在该代码中,每当用户将鼠标悬停在一本书的图像上方时,就会显示三个选项(购买、添加到购物车、添加到愿望列表)。问题是,当我将鼠标悬停在任何图像上方时,所有书籍的所有选项都会同时显示。有没有办法只显示光标所在的特定书籍的选项 let images=document.getElementsByClassName('images'); 让options=document.getElementsByClassName('options'); for(选项的常

我一直在尝试编写一个javascript代码,在该代码中,每当用户将鼠标悬停在一本书的图像上方时,就会显示三个选项(购买、添加到购物车、添加到愿望列表)。问题是,当我将鼠标悬停在任何图像上方时,所有书籍的所有选项都会同时显示。有没有办法只显示光标所在的特定书籍的选项

let images=document.getElementsByClassName('images');
让options=document.getElementsByClassName('options');
for(选项的常量索引)
{index.style.display='none';}
对于(索引=0;索引{
for(选项的常量迭代器){
iterator.style.display='inline block';
}
});
}
对于(索引=0;索引{
for(选项的常量迭代器){
iterator.style.display='none';
}
});
}
滑块{
显示器:flex;
证明内容:之间的空间;
利润率:25px 0px;
}
.book{显示:内联块;位置:相对;}
.book a{显示:块;宽度:20vw;}
.info{
显示:块;
文本对齐:居中;
文字装饰:无;
颜色:rgb(90,90,90);
字号:17px;
字号:500;
过渡:颜色250ms;
}
.info:hover{color:rgb(255,0,0);}
.book a:悬停{颜色:rgb(255,0,0);}
.book.info:类型{margin top:5px;}的最后一个
.book_price{文本对齐:居中;边距:10px 0px;}
.书价{
显示:内联;
颜色:rgb(90,90,90);
字号:17px;
字号:500;
利润率:10px 5px;
}
.book_价格范围:p类型的最后一个{颜色:rgb(255,0,0);}
.选项{
背景色:rgba(255、255、255、0.7);
显示:内联块;
位置:绝对位置;
底部:250px;
右:62px;
填充:10px;
}
.备选方案a{
显示:内联块;
颜色:rgb(90,90,90);
字体大小:22px;
填充物:5px10px;
宽度:自动;
过渡:颜色250ms;
}
.选项a:n类型(2){右边框:1px实心rgb(50,50,50);左边框:1px实心rgb(50,50,50);}

₹;200.00

₹;160.00

₹;400.00

₹;260.00

₹;300.00

₹;150.00

₹;300.00

₹;150.00

₹;300.00

₹;150.00

₹;300.00

₹;150.00


您必须使用事件对象和属性目标:

for (index = 0; index < images.length; index++) {
    images[index].addEventListener('mouseover', (event) => {
        for (const iterator of options) {
            event.target.style.display = 'inline-block'; // Use event.target
        }
    });
}
for(index=0;index{
for(选项的常量迭代器){
event.target.style.display='inline block';//使用event.target
}
});
}

您必须使用事件对象和属性目标:

for (index = 0; index < images.length; index++) {
    images[index].addEventListener('mouseover', (event) => {
        for (const iterator of options) {
            event.target.style.display = 'inline-block'; // Use event.target
        }
    });
}
for(index=0;index{
for(选项的常量迭代器){
event.target.style.display='inline block';//使用event.target
}
});
}

可能还会删除
for
循环,现在似乎没有任何用途?我现在知道该使用哪种方法,但是,您上面写的代码片段更改了图像本身的显示,而不是元素“选项”,可能还会删除
for
循环,现在似乎没有任何用途?我现在知道该使用哪种方法,但是,上面编写的代码片段更改了图像本身的显示,而不是元素“选项”