简体   繁体   English

mouseover和mouseout时如何使用forEach进行显示和隐藏?

[英]How to use forEach for display and hide when mouseover and mouseout?

I create the HTML code.我创建了 HTML 代码。 now i want to use forEach in JS when the mouse over, but when i use forEach method it just happen for first item not the others.现在我想在鼠标悬停时在 JS 中使用 forEach,但是当我使用 forEach 方法时,它只发生在第一项而不是其他项。 i want its happen for each one when mouseover and mouseout.我希望它在鼠标悬停和鼠标悬停时发生。

my code link: codepen.io/A_Mahdi28/pen/bGvOebr我的代码链接: codepen.io/A_Mahdi28/pen/bGvOebr

how do i use forEach method do it on all items?我如何在所有项目上使用 forEach 方法?

Please help me.请帮我。

With the given informations the following code may help you to understand the events you need for mouseover / mouseout.使用给定的信息,以下代码可以帮助您了解 mouseover / mouseout 所需的事件。

In the HTML I have added "onmouseover / onmouseout" to your code pointing to the functions show/hide.在 HTML 中,我在代码中添加了“onmouseover / onmouseout”,指向函数显示/隐藏。 Inside those functions you can now execute your code for these events.在这些函数中,您现在可以为这些事件执行代码。 If you are targeting a special div you have to also pass some parameters so you can identifiy the div you want to target.如果您要定位一个特殊的 div,您还必须传递一些参数,以便您可以识别要定位的 div。

 const mainProduct = document.querySelector('.main_product'); const mainInfo = document.getElementById('main_info'); mainProduct.classList.add('hidden'); mainInfo.style.filter = 'blur(2px)'; function show(target) { console.log(target); mainProduct.classList.remove('hidden'); mainInfo.style.filter = 'blur(0)'; } function hide(target) { console.log(target); mainProduct.classList.add('hidden'); mainInfo.style.filter = 'blur(2px)'; }
 <div href="#" class="review_book" onmouseover="show(event.target)" onmouseout="hide(event.target)"> <div id="main_info"> <img src="/img/Reviews/1984.jpg" alt="1984 Cover" /> <div class="main_description"> <p class="book_name">1984</p> <p class="book_author">George Orwell</p> </div> </div> <div class="main_product hidden"> <div href="#"> <button class="btn see_review--btn"> <i class="ph-eye"></i> </button> </div> <button class="btn add_wishlist--btn"> <i class="ph-bookmark-simple"></i> </button> <button class="btn add_cart--btn"> <i class="ph-shopping-cart"></i> </button> </div> </div>

So if you want to target a specific div you can either add an index to the classlist or pass the event directly in the mouseover.因此,如果您想定位特定的 div,您可以向类列表添加索引或直接在鼠标悬停中传递事件。 If you now check those events you should be able to extract the main_product and main_info of that event and manipulate the css of those.如果您现在检查这些事件,您应该能够提取该事件的 main_product 和 main_info 并操纵其中的 css。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM